Reverse


#1



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


I do not understand why I am gettin a list instead of a string


I do not understand why I am getting ['!',...'P'] instead of a str


def reverse(text):
new = []
num = []
test = len(text)
for i in range(test):
new.append(i)
tot = sorted(new, reverse=True)
for j in tot:
num.append(text[j])
h = str(num)
toto = ''.join(h)
return toto


#2

Try refreshing the lesson. Also h = str(num) seems a little extra as num is already a list of strings. Just the ''.join() is enough. That's just my opinion.


Anti_vowel
#3

The problem is that toto becomes the string of your list num.
toto=''.join(num) will work.
Try the following:
add

a=[]
a.append(h)
a.append(1)

after h=str(num)
you will see, that the list a will contain a single string and an integer: "['!', 'n', 'o', 'h', 't', 'y', 'P']", 1. So h is not a list of strings, but a string which looks like a list, but num is the list.
So at the end you get toto as a joint string but your input (h) is already a string.
I hope you get what I'm trying to say.

Greetings Kiluah


#4

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