Reverse


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096


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


how do i convert the list into a string

def reverse(text):
    d=list(text)
    new=[]
    b=len(text)-1
    for n in d:
        new.append(d[b])
        b=b-1
    return new
    
    ans=""
    ans.join(new)
    return ans

print reverse('python!')


#2

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


#3

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


#4

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

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


#5

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