From the python.org tutorial
Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size
All it does is slice. You pick. start stop and step so basically you're saying it should start at the beginning until the beginning but going backwards (-1).
If you do it with -2 it will skip letters:
>>> a[::-2]
'olh'
When doing [0:5:-1]
your'e starting at the first letter and going back directly to 5 and thus it will stop. only if you try [-1::-1]
will it correctly be able to go to the beginning by doing steps of negative 1.
Edit to answer comments
As pointed out the documentation says
an omitted second index defaults to the size of the string being sliced.
Lets assume we have str
with len(str) = 5
. When you slice the string and omit, leave out, the second number it defaults to the length of the string being sliced, in this case - 5.
i.e str[1:] == str[1:5]
, str[2:] == str[2:5]
. The sentence refers to the length of the original object and not the newly sliced object.
Also, this answer is great