Python Strings Slicing

A slice represents a part or piece of a string. The format of slicing is:

CopiedCopy Code
stringname[start: stop: stepsize]

If 'start' and 'stop' are not specified, then slicing is done from 0th to n-1th elements. If 'stepsize' is not written, then it is taken to be 1.

str = 'Core Python'

str = 'Core Python'

Python Strings Slicing

String slice Example:

CopiedCopy Code

print( str[0:9:1] )   #'Core Pyth'
print( str[0:9:2] )   # 'Cr yh'
print( str[::] )   # 'Core Python'
print( str[2:4:1] )   # 're'
print( str[::2] )   #'Cr yhn'
print( str[2::] )   # 're Python'
print( str[:4:] )   # 'Core'
print( str[-4:-1] )   # 'tho'
print( str[-6::] )   # 'Python'
print( str[-1:-4:-1] )   # 'noh'
print( str[-1::-1] )   # 'nohtyPeroC'
print( str[::-1] )   # 'nohtyPeroC'