 
                  In this tutorial, we will learn the slice notation in python. As we know slice means ‘part of’. So we can say slicing is the process of extracting part of the string. Not only on a string, but we can also perform slice operation on any sequential data types like byte, byte arrays, List, Tuple, and ranges. Slice notation in python returns a list or portion of a list. We will understand this by solving different examples.
Below shows the syntax of python’s slice notation.
list[start:stop:step]
Note: If the step value is positive, move in the forward direction. If the step value is negative, move in the backward direction.
Let’s perform a slice operation on a string.
        a = "python"  
for i in range(1,3,2):  
    pass
        
#consider string
s="PYTHON PROGRAMMING"
#with step value 2
result_1=s[1::2]
print(result_1)
#with step value -2
result_2=s[9::-2]
print(result_2)
#without step value
result_3=s[5::]
print(result_3)
#without start, stop, step value
result_4=s[::]
print(result_4)
The output of this program is below as follows.
YHNPORMIG
OPNHY
N PROGRAMMING
PYTHON PROGRAMMING
Let’s perform a slice operation on a List .
#Intializing list
#Performing slice operation on List DataType
list_1=[2,5,8.0,"Python",414]
print("The list is:",list_1)
list_2=list_1[0:2:1]
print("The sliced list is:",list_2)
list_3=list_1[1::2]
print("The sliced list is:",list_3)
The output of this program is below as follows.
The list is: [2, 5, 8.0, 'Python', 414]
The sliced list is: [2, 5]
The sliced list is: [5, 'Python']
Let’s perform a slice operation on a Tuple .
#Intializing tuple
#Reverse operation using slice
Tuple_1=(1,2,3,4,5,6,7,8,9,10)
print("The original tuple is:",Tuple_1)
print("The sliced tuple is:")
print(Tuple_1[-1:-11:-1])
print(Tuple_1[-1::-1])
print(Tuple_1[::-1])The output of this program is below as follows.
The original tuple is: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
The sliced tuple is:
(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
In this tutorial, we have learned about the slice operator and how it works on data types. We solved examples by performing slice operations on string, list, and tuple data types. Try to solve more examples by giving different start, stop and step values and discuss the output.