The List,tuple,string,bytes object can be sliced to produce a new object of these classes.
Format: Li[start:stop:step]
List slice:
1>>> Li = ['Michael','Sarah','Tracy','Jack','Black']2>>>Li[0]3 'Michael'4>>>li[-1]5 'Black'6>>>li[1:3]7['Sarah','Tracy']8>>>li[:3]9['Michael','Sarah','Tracy']Ten>>>li[-2:] One['Jack','Black'] A>>>li[:] -['Michael','Sarah','Tracy','Jack','Black'] ->>>li[::2] the['Michael','Tracy','Black']
Tuple slices:
1>>> Li = ('Michael','Sarah','Tracy','Jack','Black')2>>>Li[0]3 'Michael'4>>> li[-1]5 'Black'6>>> Li[1:3]7('Sarah','Tracy')8>>> li[-2:]9('Jack','Black')Ten>>>li[:] One('Michael','Sarah','Tracy','Jack','Black') A>>> Li[::2] -('Michael','Tracy','Black')
String Slice:
1>>> s ='ABCDEFGH'2>>>S[0]3 'a'4>>> s[-1]5 'h'6>>> S[1:3]7 'BC'8>>> s[-2:]9 'GH'Ten>>>s[:] One 'ABCDEFGH' A>>> S[::2] - 'Aceg'
Bytes slices (Note: Python3 has bytes type, python2 not):
1>>> A = b'abcdef'2>>>A[0]3974>>> a[-1]51026>>> A[1:3]7B'BC'8>>> a[-2:]9B'EF'Ten>>>a[:] OneB'abcdef' A>>> A[::2] -B'Ace'
Python slice slices