reverse('Pyhton!') is returned as a list instead of a string

how do i convert the list into a string

def reverse(text):
    for n in d:
    return new
    return ans

print reverse('python!')


using join, like you do. Except, you don't store the result of your join action anywhere


First of all, your function ends at the first return call, so the join() method never gets called!

Next, you need to capture the result of doing ans.join(new) : as a method join() does not change anything but it returns the string. Try something like newText = ans.join(new) or even return ans.join(new) which saves a step.

I also noticed there is some funny looping going on: although you are using n to loop through d , you don't seem to be using it. You could just be adding each character to the front of what you already have, something like

    for n in d:
        new = [n] + new

    return "".join(new)

Hope that helps

Tim F


thanks so much. I figured a simple way of doing it

def reverse(text):
new=" "
for char in text:
new= new+text
return new


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