# 7/15 Reverse - Can you explain how the "l = len(text) - 1" works here please?

#1

I am trying to understand this and just need a better explanation that my brain can make up. How exactly is the “l = len(text) - 1” making it go through something backwards?

``````def reverse(text):
word = ""
l = len(text) - 1
while l >= 0:
word += text[l]
l -= 1
return word
``````

#2

Do you know what each individual thing does there?
I suggest executing that code manually, in your head, and observing what you do.

#3

Yes, I know what everything does. I just need it explained to me, in a way that’s not from my own head, how “l = len(text) - 1” makes it go backwards in practice. Sorry, it’s just how I learn things.

#4
• “l = len(text) - 1” doesn’t make it go backwards. It returns a number that we use to call the last index position of (text)
• For the word Python it would return 5 (the length of python (6) -1)
• The while loop uses index position 5 to call the last letter of Python, ‘n’, and add it to our blank word
• Then we count down by 1 (l -=1) (the part which makes it go backwards)
• We run it again as long as l >= 0 as stipulated in the while statement.

So. The while loop calls python[5] adds n, l counts down to 4, the loop runs again calling index 4 and adds o, l counts down to 3, the loop runs again calling index 3 and adds h, and so on until l counts down to -1 at which points the while loop knows to stop. At that point we’ve added n, o, h, t, h, and p to our word in that order, giving us python backwards

#5

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