Python slice() FunctionPython Functions


The slice() method specifies how to slice a sequence. Through this function, you can specify where to start the slicing, and where to end.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

temp = ("1", "2", "3", "A", "B", "C", "D", "E")
x = slice(0, 8, 3)
print(temp[x])

Output:

('1', 'A', 'D')

Syntax

slice(start, end, step)

Parameters

Name Description
start An integer number specifying where to start the slicing.
end An integer number specifying where to end the slicing
step An integer number specifying the step of the slicing.

Return Value

It returns a slice object.

© 2021 Programtips.org, all rights reserved. Privacy Policy | Contact Us