2
# word reverser
#user input word is printed backwards

word = input("please type a word")

#letters are to be added to "reverse" creating a new string each time
reverse = ""

#the index of the letter of the final letter of "word" the users' input
#use this to "steal" a letter each time
#index is the length of the word - 1 to give a valid index number
index = len(word) - 1

#steals a letter until word is empty, adding each letter to "reverse" each time (in reverse)
while word:
    reverse += word[index]
    word = word[:index]
    print(reverse)

print(reverse)
input("press enter to exit")

Working to make a simple program that spells a user input word backwards and prints it back to them by "stealing" letters from the original and making new strings from them. Trouble I'm having is this code spews back a string index out of range error at reverse += word[index] Help or a better way of achieving same result is mucho apreciado.

1
  • 1
    Put a print(index, len(word)) line right before the reverse +=... line and see what happens. Commented Aug 7, 2015 at 21:00

4 Answers 4

3

Reversing a word in python is simpler than that:

reversed = forward[::-1]

I wouldn't use a loop, it's longer and less readable.

Sign up to request clarification or add additional context in comments.

2 Comments

Python strings do not have a pop function.
How embarrassing, I always thing of strings as lists. Fixed, thanks!
2

While others have pointed out multiple ways of reversing words in Python, here is what I believe to be the problem with your code.

index always stay the same. Lets say the user inputs a four letter word, like abcd. Index will be set to three (index = len(word) - 1). Then during the first iteration of the loop, word will be reduced to abc (word = word[:index]). Then, during the next iteration of the loop, on the first line inside it (reverse += word[index]) you will get the error. index is still three, so you try to access index[3]. However, since word has been cut short there is no longer an index[3]. You need to reduce index by one each iteration:

while word:
    reverse += word[index]
    word = word[:index]
    index -= 1

And here is yet another way of reversing a word in Python (Wills code is the neatest, though):

reverse = "".join([word[i-1] for i in range(len(word), 0, -1)])

Happy coding!

Comments

1

You're going to want to use the "range" function.

range(start, stop, step)

Returns a list from start to stop increasing (or decreasing) by step. Then you can iterate through the list. All together, it would look something like this:

for i in range(len(word) -1, -1, -1):
    reverse += word[i]
    print(reverse)

Or the easier way would be to use string slicing to reverse the word directly and then iterate through that. Like so:

for letter in word[::-1]:
    reverse += letter
    print(reverse)

With the way it is written now, it will not only print the word backwards, but it will also print each part of the backwards word. For example, if the user entered "Hello" it would print

o
ol
oll
olle
olleH

If you just want to print the word backwards, the best way is just

print(word[::-1])

Comments

1

It is because you are not changing the value of the index

modification:

while word:
    reverse += word[index]
    word = word[:index]
    index-=1
print(reverse)`

that is you have to reduce index each time you loop through to get the current last letter of the word

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.