How do you reverse a Python string without omitting the start and end slice arguments?
word = "hello"
reversed_word = word[::-1]
I understand that this works, but how would I get the result by specifying the start and end indexes?
word = "hello"
reversed_word = word[?:?:-1]
It's hard to explain to students why word[::-1]
reverses a string. It's better if I can give them logical reasoning rather than "it's the pythonic way".
The way I explain word[::1]
is as follows: "You have not specified the start so it just starts from the start. You have not specified the end so it just goes until the end. Now the step is 1 so it just goes from the start to the end 1 character by 1." Now when my students see word[::-1]
they are going to think "We have not specified the start or the end so it will go through the string -1 characters at a time?"