| start | (optional) default = 0 , The start position of the slice |
| stop | ( required ), slice should Stop before this position |
| step | ( optional ) default =1 , step to consider for slicing |

obj_slice1=slice(2)
print(obj_slice1) # slice(None, 2, None)
obj_slice2=slice(2,4)
print(obj_slice2) # slice(2, 4, None)
obj_slice3=slice(2,4,2)
print(obj_slice3) # slice(2, 4, 2)
obj_slice=slice(2)
my_list=[5,2,1,4,9,3,6]
print(my_list[obj_slice]) # [5,2]
Now start=2, stop=5 and step=1
obj_slice=slice(2,5,1)
my_set=(5,2,1,4,9,3,6)
print(my_set[obj_slice]) # (1,4,9)
obj_slice=slice(2,5)
my_str='plus2net'
print(my_str[obj_slice]) # us2
obj_slice=slice(-5,-2)
my_str='plus2net'
print(my_str[obj_slice]) # s2n
obj_slice=slice(-7,-1,2)
my_str='plus2net'
print(my_str[obj_slice]) # lsn
obj_slice=slice(1,5,1)
print(type(obj_slice)) # <class 'slice'>
obj_slice=slice(2,5)
my_set={5,2,1,4,9,3,6}
print(my_set[obj_slice])
Error will be generated.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.