Reverse... ”String index out of range"


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


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.


Hi Ionatan,

Thanks a lot! my function works as you suggested.