Reverse


#1


Hi, could someone tell me what's wrong with my code? I know that first of all, it's too short but I don't know what's missing. I'm stuck, please help me.

The error that shows up:
Oops, try again. Your function fails on reverse("Python!"). It returns "None" when it should return "!nohtyP".


def reverse(text):
    lst = []
    text = str(text)
    for char in text:
        lst.append(text[len(text) - 1])
    print lst


#2

This should be a return statement. That's why it is returning None.

Is not text already a string?

Does not look quite right.


#3

def reverse(text):
    lst = []
    text = str(text)
    x = 1
    while x <= len(text):
        lst.append(text[len(text) - x])
        x += 1
        
    return lst

I came up with something like this, but still I don't know how to make one word from the elements of list lst.


#4

Python has a special function for joining elements to form a string (they must be string type, though. We cannot join() numbers unless they are first converted to str()

lst = ['a','b','c','d']
string = ''.join(lst)
print string    # abcd.

The syntax for join() is,

'separator in quotes' dot keyword token open paren list close paren

To join letters to form a word, the separator is the empty string, '' or "".

To join words to form a sentence, the separator is a space character, ' ' or " ".

To join elements to form a string that is a list, the sepatator would be, ', ' or ", ".


#5

Oh, I forgot about that, thanks for your help, everything works properly :slight_smile:


#6

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