Reverse


#1



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

It reverses the word but it returns it in list format, how do I change it into 1 string.

Here is the error message:

Oops, try again. Your function fails on reverse("Python!"). It returns "['!', 'n', 'o', 'h', 't', 'y', 'P']" when it should return "!nohtyP".


def reverse(text):
    x = len(text) - 1
    y = []
    while x > -1:
        y.append(text[x]) 
        x = x - 1
    else:
        
        return y


#2

you could use .join(), which would be the best way to achieve this


#3

Don't worry, just worked it out.

    y = y + text[x]

instead of

y.append(text[x])


#4

thank you
i'll try that as well


#5

you made y a string? I would personally advice against it, strings are immutable in python while lists are mutable, so using a list + built in join function is more efficient.


#6

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