Slice prototype strs = 'abcdefg'
Strs[start: end:step]
The three parameters for slicing are start, end, and step.
The first bit is subscripted to 0 and the end bit is not taken, e.g. strs[1:3] = 'bc'
If start, end are out of range of existing array, truncate by actual range strs[-100:100]='abcdefg'
When Step is empty, the default value is 1
Strs[1:5] = ‘bcde' strs[1:5:2] = ‘bd'
start<end when Step is positive, otherwise null
Strs[5:1] = ‘'
Start is null, defaults to negative infinity strs[:4] = 'abcd'
End is null, defaults to positive infinity strs[2:] = 'cdefg'
Strs[:] = ‘abcdefg'
If Step is negative, start>end, otherwise null.
Strs[1:5:-1] = ‘'
Start is null, defaults to positive infinity strs[:2:-1] = 'gfed'
End is null, defaults to negative infinity strs[4::-1] = 'edcba'
Strs[::-1] = ‘gfedcba'
python slicing
The slice operator is the sequence name followed by a square bracket with an optional pair of numbers separated by a colon. Note that this is very similar to the index operator you use. Remember that the numbers are optional and the colons are required.
The first number in the slice operator (before the colon) indicates where the slice starts, the second number (after the colon) indicates where the slice ends, and the third number (after the colon) indicates the number of slice intervals. If the first number is not specified, Python starts at the beginning of the sequence. If the second number is not specified, Python stops at the end of the sequence. Note that the returned sequence starts at the start position and ends just before the end position. That is, the start position is included in the sequence slice, while the end position is excluded from the slice.
In this way, shoplist[1:3] returns a slice of the sequence starting at position 1, including position 2, but stopping at position 3, thus returning a slice containing two items. Similarly, shoplist[:] returns a copy of the entire sequence. shoplist[::3] returns a slice of the sequence at position 3, position 6, position 9....
You can use negative numbers for slicing. Negative numbers are used in positions that count from the end of the sequence. For example, shoplist[:-1] will return a slice of the sequence that contains all but the last item, and shoplist[::-1] will return an inverted sequence slice.
Use the Python interpreter to interactively try different combinations of slice designations, i.e. you'll be able to see the results right away at the prompt. The magic of sequences is that you can access tuples, lists and strings in the same way.