Go Back

What is Slices in python

12/17/2021
All Articles

#What is slices

What is Slices in python

What is Slices? 

Slices are objects so they can be stored in variables. Some data structures allow for indexing and slicing such as lists, strings, and tuples.

We can use integers to specify the upper and lower bound of the slice or use a slice object.

s = slice(3,6)

lst = [1, 3, 'a', 'b', 5, 11, 16]

text = 'DataScience'

tpl = (1,2,3,4,5,6,7)

print(lst[s])

['b', 5, 11]

print(text[s])

aSc

print(tpl[s])

(4, 5, 6)

Article