Codecademy Forums

FAQ: Learn Python - Practice Makes Perfect - reverse

As shown in staffsar’s response, in the solution, it first converted

l = len(text) - 1
while l >= 0:
word = word + text[l]
l -= 1

In my solution, I tried to use

while len(text) >= 1:
word = word + text[len(text) - 1]
len(text) = len(text) - 1

but I get “SyntaxError: can’t assign to function call”

My question is: why doesn’t len(text) = len(text) - 1 work?

Pretty much says it all. If we wish to add to length, then we must add to the list. The len() function evaluates the length of list and returns a value. Something that returns a value cannot be assigned to, but assigned to something.

The string “Hello world” is 11 characters long (including the space). Since our condition is while l >= 0, for the loop to end at l = 0, we need (10 to 0) eleven subtractions, with initial value l =10.