7. Reverse - how to use .join


Ok, so I solved the problem with a different code, but below is the code I failed with.
I cannot join the letters in the list together. How can I join them together using .join?

def reverse(text):
    product = []
    for i in range(len(text)-1,-1,-1):
    " ".join(product)  
    return product


you can store the result of the join into a variable:

product = " ".join(product)

or return it directly:

return " ".join(product)

you where so close


Could you explain what this does? I'm not sure I understand what's happening:

for i in range(len(text)-1,-1,-1):


range has 3 values:

strings are zero indexed based, while length start counting at one, so the last character is is indexed at length -1 (range start value), range does not include the stop value so you need -1 to get the character at index 0. and you want to loop from high to low so the steps needs to be -1


Thanks. I'm still getting my head wrapped around it but that helps. I was assuming that the process steps up from low to high.


there is one simply way to find get good insight:

x = "hello"
for i in range(len(x)-1, -1, -1):
   print i, x[i]

see what the loop does by adding print statements


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