From the python documentation docs.python.org/tutorial/introduction.html#strings:
Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.
For the standard case, this makes a lot of sense:
>>> s = 'mystring'
>>> s[1:]
'ystring'
>>> s[:3]
'mys'
>>> s[:-2]
'mystri'
>>> s[-1:]
'g'
>>>
So far, so good. However, using a negative step value seems to suggest slightly different defaults:
>>> s[:3:-1]
'gnir'
>>> s[0:3:-1]
''
>>> s[2::-1]
'sym'
Fine, perhaps if the step is negative, the defaults reverse. An ommitted first index defaults to the size of the string being sliced, an omitted second index defaults to zero:
>>> s[len(s):3:-1]
'gnir'
Looking good!
>>> s[2:0:-1]
'sy'
Whoops. Missed that 'm'.
Then there is everyone's favorite string reverse statement. And sweet it is:
>>> s[::-1]
'gnirtsym'
However:
>>> s[len(s):0:-1]
'gnirtsy'
The slice never includes the value of the second index in the slice. I can see the consistency of doing it that way.
So I think I am beginning to understand the behavior of slice in its various permutations. However, I get the feeling that the second index is somewhat special, and that the default value of the second index for a negative step can not actually be defined in terms of a number.
Can anyone concisely define the default slice indices that can account for the provided examples? Documentation would be a huge plus.