slice() in Python
Introduction
To extract a slice of the elements from the collection of elements, use the slice() method in Python. Python comes with two redundant slice routines. While the second method needs three arguments and returns a slice object, the first function only requires a single argument. You can retrieve a part of the collection using this slice object. Slice can be applied here, for instance, if we want to get the first two elements from a list of 10 components. The function's signature is provided below.
Syntax
slice (stop) |
Parameters
start: Slicing index at where to begin.
stop: Slice's final index
Step: The number of steps needed to jump.
Return
A slice object is the result.
Examples
To comprehend the functioning of the slice() function, let's look at some samples:
Example 1:
# Python slice() function example |
Output:
slice(None, 5, None) |
Example 2:
# Python slice() function example |
Output:
Brnn |
Example 3:
# Python slice() function example |
Output:
(45, 1214, 636) |
Example 4:
# Python slice() function example |
Output:
(45, 1214, 636) |