Reverse str does not support asignment


Traceback (most recent call last):
File "python", line 10, in
File "python", line 6, in reverse
TypeError: 'str' object does not support item assignment

I expected that assignment statement works for all type of objects

def reverse(text):
    length = len(text)
    temp = 's'
    for i in range(0, (length/2)):
        print i
        text[i] = text[length-i-1]
        length[i-1] = temp
    return text

print reverse("LoL")


strings are immutable in python, so here:

 text[i] = text[length-i-1]

you can't assign something to a string at a specific index


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