Traceback (most recent call last):
File "python", line 8, in
File "python", line 6, in reverse
IndexError: string index out of range

i can't seem to understand what is wrong here.Please help.

def reverse(text):
    x = len(text)
    new_word = ""
    for i in range(0,x+1):
        i = x 
        new_word = new_word + text[i]
        x -= 1


well, if we have hem , the string has a length of 3, however, strings are zero indexed based so the indexes are: 0, 1, 2. In other words, 3 is no longer a valid index, hence the indexerror


Thank You:slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.