Reverse... ”String index out of range"


#1

my code is as below. it throw a error that "String index out of range". I checked and seems it should be ok. Could anyone help to point my mistake? Thank you!

def reverse(text):
    string = ""
    for i in range(len(text)):
        string[i] += text[len(text) - 1 - i]
    return string

#2

string is empty and thus has no valid indexes. Strings are also immutable, so you can't change them anyway.
Put the characters in a list and then join those characters into a string once they're all in place.


#3

Hi Ionatan,

Thanks a lot! my function works as you suggested.