Reverse error


#1



Hello,
My code threw a "'str' object does not support item assignment" error.
Could anyone help me with it?
Thanks!!


def reverse(text):
    i = 0
    while i<=len(text)//2:
        s = text[i]
        text[i] = text[len(text)-i-1]
        text[len(text)-i-1] = s
        i += 1


#2

Yah!

text[i] = text[len(text)-i-1]

remember ?
It is not possible!
String is an immutable object!

x = "devmaster06078"
x[2] = 'n'

the above code will throw an error!

Update** Hints..

Create an empty list/string
Then iterate over original string and add items to already defined empty list/string.
I will leave rest for you to figure out.
I hope it works for you :slight_smile:
Do tell if it solved your problem or not!


#3

Thank you so much! I understand now!
I created an empty and it worked! :slight_smile:


#4

Your'e welcome! :slight_smile:
keep up the good work! :slight_smile: :thumbsup:


#5

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