Python slice() function

Published February 17, 2022

The slice() function returns a part of an iterable as an object of the slice class depending on the provided range. It may be used with range objects, list, set, tuple, bytes, strings, and customised class objects that support the len() and getitem()  sequence functions.

The Syntax of the slice()
The slice() method is written as follows:

slice(stop)
slice(start, stop, step)

The slice() function supports three parameters:
start: The index at which the object slicing begins.
Stop: The index at which the object slicing comes to an end.
Step: An optional input controls the slicing increment among each index.
Let's take a look at the  example of the slice().


Example
 

my_string = 'ILovePython'
slice_1 = slice(1)
slice_2 = slice(4, 5)
print("String slicing")
print(my_string[slice_1])
print(my_string[slice_2])


Output

String slicing
I
e

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

244 Views