Reverse the string


def reverse(text):
for i in range(len(text)):
return text
"what is wrong in this code plz help

Replace this line with your code.


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 16, in reverse
TypeError: 'str' object does not support item assignment

This is telling us that,

text[i] = ...

is not a valid assignment. In Python, strings are immutable. To use your method, we will have to revert to a list.

def reverse(text):
    txt = list(text)
    for i in range(len(text)):
        txt[i] = text[(len(text) - i ) - 1]
    return ''.join(txt)


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