What is the meaning of string[::-1][::-1][::-1]?

Viewed 40
word = "hello"
word[::-1][::-1][::-1]

I see that it reverses word with slices when put through an interpreter, but how exactly does this work with three []?

1 Answers
word = "hello"
word[::-1][::-1][::-1]

This is equivalent to:

word = "hello"
word = word[::-1]
word = word[::-1]
word[::-1]

Easy subscript ([]) works on the results of the previous. We could also make this more explicit with parentheses.

word = "hello"
( ( word[::-1] )[::-1] )[::-1]

In this case, the string is reversed, then that reversed string is reversed back to the original order, then it's reversed again. The word "overkill" comes to mind.

Related