Reverse String Function


#1

def reverse(text):
rev = ''
for i in range(len(text), -1, -1):
rev += text[i-1]
return rev

Why won't this code pass? It says the output is "!nohtyP" and it expects "!nohtyP" so I have no clue why it won't pass!


#2

try this

def reverse(text):
    rev = ''
    for i in range(len(text), 0, -1):
        rev += text[i-1] 
    return rev
    
    
print reverse("code")

the problem is your code due to this line

 range(len(text), -1, -1)

was returning the last character of the original text at the beginning and end of the reversed text


#3

Thanks, silly me! I will be more careful next time.