Practice Makes Perfect - reverse


#1

What's wrong with this???

def reverse(text):
     newtext = ""
    for letter in text:
        newtext.insert(newtext[0],letter)
    text = newtext
    return text

I get a " Your code looks a bit off--it threw a "'str' object has no attribute 'insert'" error"
please help :slight_smile:


#2

strings are immutable in python, .insert() is for list


#3

Thank you very much! Is there a way to define my result variable as a list and then change it into a string?


#4

well, you can always loop over the list (contain the reversed string) and add them (+) to a string, but personally i would use .join() for this


#5

Thanks! I'll try it.


#6

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