# FAQ: Code Challenge: String Methods - Reverse

This community-built FAQ covers the “Reverse” exercise from the lesson “Code Challenge: String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

19 posts were split to a new topic: How can this be solved without using range()?

2 posts were split to a new topic: My solution

Is it a program error??

The answer required and the answer my code resulted out was the same. However, the system is not passing me with my code.

[spoiler]This text will be blurred[/spoiler]
# Write your reverse_string function here:
def reverse_string(word):
reversed_str = " "
for letter in word:
reversed_str = letter + reversed_str
return reversed_str
# Uncomment these function calls to test your  function:
# should print ymedacedoC
print(reverse_string("Hello world!"))
# should print !dlrow olleH
print(reverse_string(""))
# should print
[spoiler]This text will be blurred[/spoiler]

That space character will not be expected in the result so could result in a fail. Try using an empty string, initially.

reversed_str = ""
2 Likes

Thank you sir. That worked!

2 Likes

Alternative solution:

def reverse_string(word):
a=""
lenW=len(word)
for i in range(lenW):
a+=word[lenW-i-1]
return a

here’s a little trick i just learned:

def reverse_string(word):
reverse_word = word[::-1]
return reverse_word

# should print ymedacedoC

print(reverse_string(“Hello world!”))

# should print !dlrow olleH

print(reverse_string(""))

# should print

2 Likes

Hello, so for everybody who didn’t really understand like i didn’t why Codecademy’s solution is with -1 in the middle of range-function:

def reverse_string(word):
reverse = ""
for i in range(len(word)-1, -1, -1):
reverse += word[i]
return reverse

It is because range will exclude not the highest number wich is len(word) but in this situation the lowest, which is -1. So that’s why at the beginning we subtract from the “len(word)”, because range will include the start of the range and exclude the end of the range (i suppose). And of course it sounds a little bit obvious duh, but for me it was slightly confusing because of all this reverse moving.

1 Like

Hey… This works.

def reverse_string(word):

return word[::-1]

1 Like

This code is giving the correct answer but the page says its incorrect and doesn’t allow me to move to next problem.

def reverse_string(word):

reverse = “”

for i in range(-1,-len(word),-1):

reverse += word[i]

return reverse

Hello @bipinthapa448!

It seems that your code doesn’t return the whole word reversed.

reverse_string("hello") # returns olle

How could you change your code to return the entire reversed word?

Hello everyone! I finally got this version of code to work for the Reverse exercise:

def reverse_string(word):
reversed_word = “”
for index in range(1, len(word) + 1):
reversed_word += word[index * -1]
return reversed_word

I discovered that it works through trial and error, but I’m not understanding why I needed to add 1 to len(word) in line three. Since I’m iterating through indexes backward through the word, the indexes start at abs|1| (-1), not 0. I thought the answer would have been simply:

range(1, len(word))

without needing to add anything to len(word). Any insight is greatly appreciated. Thanks!
-Justin

Remember that range(start, stop[, step]) starts at start and ends at stop - 1.

>>> for i in range(1, 5):
...    print(i)
...
1
2
3
4

The last character of a string is at index -1 (not -0, since this would just be 0, indicating the first character). This means that the last character appended to reversed_word would be at index:

• -len(word) if stop was len(word) + 1
• -len(word) - 1 if stop was len(word)

Since the first character in a string has the index -len(word), we would want the first of the above two options to be the last character appended.

Thank you, victoria_dr!

1 Like

Hello, I know a lot of people have explained a lot of aspects of this so apologies if it sounds redundant but I still don’t understand how the function works…

def reverse_string(word):
reverse = “”
for letter in range(len(word)-1, -1,-1):
reverse += word(letter)

mainly, i can’t visualize how the for loop is working with the negative indices.
the len(word)-1 should access the last index of the parameter, correct?
the 2nd and 3rd (-1) in range is where i am lost…
i’d appreciate if anyone can clarify this a bit, i can skip the exercise but i feel it’s very important to understand these.

thanks.

range(start, stop, step)

The inputs are all integers (required). Only the stop value is needed if the start is 0 and the step is 1. The stop value is excluded from finished range object.

range(10)  =>  range(0, 10)

which when iterated (or cast to a list) will result in the sequence,

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

To generate the opposite sequence,

range(10 - 1, -1, -1)

9, 8, 7, 6, 5, 4, 3, 2, 1, 0

The stop of -1 is excluded, so we end on zero.

5 Likes

Thank you very much sir! It’s crystal clear

1 Like

Found another simiar one without the range:

def reverse_string(word):
output_string = ""
a = -1
for i in word:
output_string += word[a]
a = a - 1
return output_string

Hello, I’m a new learner and really have a problem with code. Concerning string methods - reverse this is my try:

def reverse_string(word):
new_word = “”
for i in range(len(word)-1, 0, -1):
new_word += word[i]
return new_word