A sequence of object of any type(string, bytes, tuple, list or range) or the object which implements
__len__() method then this object can be sliced using
- slice(start, stop, step)
start: Starting index where the slicing of object starts.
stop: Ending index where the slicing of object stops.
step: It is an optional argument that determines the increment between each index for slicing.
Return Type: Returns a sliced object containing elements in the given range only.
Note: If only one parameter is passed then start and step is considered to be
String slicing Gee ek List slicing [1, 2, 3] [2, 4] Tuple slicing (1, 2, 3) (2, 4)
In Python, negative sequence indexes represent positions from the end of the array. slice() function can also have negative values. In that case, the iteration will be performed backward i.e from end to start.
String slicing GeeksforGe se List slicing [1, 2] [5, 3] Tuple slicing (1, 2) (5, 3)
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course