Slicing in Python is a mechanism to
select a range of items from Sequence types like strings, list, tuple, etc.
Example
of slicing:
>>>
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">>> l[1:3]
[2, 3]
>>> l[1:-2]
[2, 3]
>>> l[-3:-1] # negative indexes in slicing
[3, 4]
>>> s[1:3]
'el'
>>> s[:-5]
'Hello '
>>> s[-5:]
'World'
No comments:
Post a Comment