Reverse: is text[-] allowed?


Below is my code and it works. But does text[-letter] work the same as [::-1] and therefore I should try a different method?

def reverse(text):
    reverse_word = ""
    for letter in range(1, len(text)+1):
        reverse_word += text[-letter]
    return reverse_word


Both examples,

are of the same thing, slicing.

>>> "reverse"[-1]
>>> "reverse"[-2]
>>> "reverse"[-3]
>>> "reverse"[-4]
>>> "reverse"[-5]
>>> "reverse"[-6]
>>> "reverse"[-7]

More reading:


your method is fine? the use of -letter is allowed, you still have to code a loop correctly. with [::-1] you have to do nothing at all, then you just get:

def reverse(text):
   return text[::-1]


Your solution is a really elegant one. You should be happy with it :slight_smile:


A post was split to a new topic: Reverse


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