Home » Python slice() function with Examples

Python slice() function with Examples

by Online Tutorials Library

Python slice() Function

Python slice() function is used to get a slice of elements from the collection of elements. Python provides two overloaded slice functions. The first function takes a single argument while the second function takes three arguments and returns a slice object. This slice object can be used to get a subsection of the collection. For example, if we want to get first two elements from the ten element?s list, here slice can be used. The signature of this function is given below.

Signature

Parameters

start: Starting index of slicing.

stop: End index of the slice

step: The number of steps to jump.

Return

It returns a slice object.

Let’s see some examples of slice() function to understand it’s functionality.

Python slice() Function Example 1

Output:

slice(None, 5, None)  slice(0, 5, 3)  

Python slice() Function Example 2

Output:

Jaot  toa  

Python slice() Function Example 3

Output:

(45, 1214, 636)  (66, 41, 68)  

Python slice() Function Example 4

Output:

(45, 1214, 636)  (45, 1214, 636)  

Next TopicPython Set

You may also like