a = 'ABCDEFG' a[:3] -> 'ABC' a[2:5] -> 'CDE'
And more interestingly:
a[-3:] -> 'EFG'
a[6:4:-1] -> 'GF'
But you can see that the reverse slicing is starting to stretch the fence-post we are familiar with. Python uses zero based, inclusive-exclusive indexing. This corresponds to a C syntax of
(for i = n; i < m; i++). When you reverse it the slice goes
(for i = m - 1; i > n - 1; i--).
As you can imagine this starts to get ugly and at one point it gets to be wrong:
Say, as is often the case, you are not taking static, pre-determined slices but rather slices determined at runtime. Say you are taking slices between
The forward slice is
The backward slice is
a[m-1:n-1:-1]right? Because of the fence posts?
Well yes, except what happens when
n = 0? The forward slice is fine but the reverse slice resolves to
This is where Python becomes a little too clever. As you will recall from our earlier examples, negative indices indicate offsets from the end of the object. So, the last slice returns empty.
The correct slice is
a[m-1::-1]and the logic for this is cumbersome:
a[m-1:n-1 if n > 0 else None:-1]
The simpler way is to do