-1 step just reverts the direction of the slice:
>>> x = "Hello World !"
>>> x[6]
'W'
>>> x[2]
'l'
>>> x[6:2:-1]
'W ol'
[6:2:-1] means give me a slice from the 6th to the 2nd item (not including), reversed.
FYI, you don't need python installed for checking the result of the code you've asked about, go to pythonanywhere and play:
PythonAnywhere is a Python development and hosting environment that displays in your web browser and runs on our servers.
Also see:
- Explain Python's slice notation
- Extended Slices
Videos
The easiest way to explain is probably to address your examples:
"123456"[::-2]
# This takes the whole string ([::])
# Then it works backward (-)
# and it does every other character (2)
"123456"[1::-2]
# This is also working backward (-)
# every other character (2)
# but starting at position 1, which is the number 2.
"123456"[2::-2]
# Again, working backward (-)
# Every other character (2)
# begin at position 2, so you end up with positions 2, and 0, or '31'
The slicing syntax is [<start>:<end>:step]. If <start> is omitted and the step is negative then it starts at the end of the string.
Remember that indices start from zero. With this in mind, it may be clearer if you change your string to '012345':
In [1]: '012345'[1::-2]
Out[1]: '1'
In [2]: '012345'[2::-2]
Out[2]: '20'
In [3]: '012345'[5::-2]
Out[3]: '531'
As you can see, the slice does start from the correct index and does take the correct (negative) stride at each step until in reaches the beginning of the string.