Python Slicing Operations In English Language
Please don't forget to subscribe to my channel Thank you very much.
After watching this video and content you will never fail in python slicing operation. There are three types of slicing operations available. Once you are aware that your work goes very simple.
- Type-1
- Type-2
- Type-3
Type-1:
This type is very simple and it contains only one index number. This number is either a positive number or negative. This number is nothing but the position of the element. You don't need to concentrate on the directions.
Examples:
text = "regularpython" print('text[3] = ', text[3]) print('text[5] = ', text[5]) print('text[-3] = ', text[-3]) print('text[-4] = ', text[-4])
Type-2:
This type has two index positions. The first position represents the starting point of the index and the second one shows the ending point of the index. We should always take the elements in a positive direction only. Positive direction means left to right direction only.
Note: If the endpoint is given, then we should delete the element value at that index position.
Examples:
text = "regularpython" print('text[2 : 5] = ',text[2 : 5]) #= ‘gul’ print('text[5 : 2] = ', text[5 : 2]) #= ‘’ print('text[-5 : -2] = ', text[-5 : -2]) # = ‘yth’ print('text[-2 : -5] = ', text[-2 : -5]) # = ‘’ print('text[3 : -2] = ', text[3 : -2]) #= ‘ularpyth’ print('text[-2 : 3] = ', text[-2 : 3]) # = ‘’ print('text[2 : ] = ', text[2 : ]) #= ‘gularpython’ print('text[ : 2] = ', text[ : 2]) #= ‘re’ print('text[-4 : ] = ', text[-4 : ]) #= ‘thon’ print('text[ : -5] = ', text[ : -5]) # = ‘regularp’
Type-3:
This type is a little bit tough to understand. But if you understand the directions then it becomes very easy for you. There are three index values are given. The first one shows the starting point, the second one shows the ending point and the third one shows the steps. The first two index positions behave like type-2. But the last number is very important in this type-3. If the last number is positive then it indicates, take the elements from left to right. If the number is negative then it takes elements from right to left. If the number is 2 then jump 2 steps and repeat the process until we reach the end position.
Examples:
text = "regularpython" print('text[2 : 3 : 1] = ', text[2 : 3 : 1]) #= ‘g’ print('text[-6 : -2 : 2] = ', text[-6 : -2 : 2]) #= ‘pt’ print('text[-7 : -2 :-1] = ', text[-7 : -2 :-1]) #= ‘’ print('text[2 : -2 :-2] = ', text[2 : -2 :-2]) #= ‘’ print('text[-2 : -2 :-2] = ', text[-2 : -2 :-2]) #= ‘’ print('text[0 : 10 : 3] = ', text[0 : 10 : 3]) #= ‘ryrt’ print('text[2 : -5 : 3] =', text[2 : -5 : 3]) #= ‘ga’ print('text[-2 : -11:-2] = ', text[-2 : -11:-2]) #= ‘otpau’ print('text[1 : : 2] = ', text[1 : : 2]) #= ‘euapto’ print('text[-3: : -2] = ', text[-3: : -2]) #= ‘hyrlgr’ print('text[ : 5 : 1] = ', text[ : 5 : 1]) #= ‘regul’ print('text[ : -3 : -2] = ', text[ : -3 : -2]) #= ‘n’