Question:

What Is Slicing In Python? Explain With Example.

Answer:

Slicing in Python is a mechanism to select a range of items from Sequence types like strings, list, tuple, etc.

>>> l=[1,2,3,4,5]
>>> l[1:3]
[2, 3]
>>> l[1:-2]
[2, 3]
>>> l[-3:-1]      # negative indexes in slicing
[3, 4]
>>> s="Hello World"
>>> s[1:3]
'el'
>>> s[:-5]
'Hello '
>>> s[-5:]
'World'

 


Keywords:

© 2017 QuizBucket.org