Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Slicing in Python programming language

Slicing in Python programming language

Slicing

Slicing in Python programming language is a method of extracting elements of sequence from a starting point t ending point. Slicing can be performed on any of the data-type like list, tuple, string, etc. Slicing takes three arguments where any one is mandatory and rests two are optional. We generally pass indexes of the elements of a sequence to get the desired output.

Slicing in python

Advantages of Slicing

  • An efficient way of iterating through the sequence.
  • Can reverse any sequence easily.
  • Formatting of sequence elements from a starting point to the ending point in an efficient way.

Slicing in different data-type

Slicing in List

The list is an iterable data-type and hence we can slice the list. The list is the same as an array in another programming language. By using slicing we can traverse through the whole list, we can reverse it and print it from a specific start index to ending index.

Slicing in list

Python code for slicing in the list

arr = ['string', 1, 9, 5.12, 6]

#Slicing in list with 2 arguments
print(arr[1:4])

#slicing in list with 1 argument
print(arr[2:])
print(arr[:3])
print(arr[::1])

#Slicing in list with 3 arguments
print(arr[1:3:1])
print(arr[1:3:-1])

#reversing an string by Slicing
print(arr[::-1])
Output:

[1, 9, 5.12]
[9, 5.12, 6]
['string', 1, 9]
['string', 1, 9, 5.12, 6]
[1, 9]
[]
[6, 5.12, 9, 1, 'string']
Slicing in String

Slicing in String

The string is an immutable data-type and so we cannot modify the string. But as the string is iterable we can access elements, reverse the string or print the elements from a starting point to an ending point. Let’s us see slicing in the string.

Python code for slicing in String

arr = 'PrepInsta'

#Slicing in list with 2 arguments
print(arr[1:4])

#slicing in list with 1 argument
print(arr[2:])
print(arr[:3])
print(arr[::1])

#Slicing in list with 3 arguments
print(arr[1:3:1])

#reversing an string by Slicing
print(arr[::-1])
Output:
rep
epInsta
Pre
PrepInsta
re
atsnIperP

Slicing in Tuple

The tuple is an immutable data-type of sequential data-type family. Tuples are iterable and we can perform operations on them. The tuple is a sequential data-type same as a list but the only difference is lists are mutable. Slicing is a good method to iterate through tuple and use tuple elements to perform operations.

Slicing in tuple

Python code for slicing in tuple

arr = tuple('PrepInsta')

#Slicing in list with 2 arguments
print(arr[1:4])

#slicing in list with 1 argument
print(arr[2:])
print(arr[:3])
print(arr[::1])

#Slicing in list with 3 arguments
print(arr[1:3:1])

#reversing an string by Slicing
print(arr[::-1])
Output:
('r', 'e', 'p')
('e', 'p', 'I', 'n', 's', 't', 'a')
('P', 'r', 'e')
('P', 'r', 'e', 'p', 'I', 'n', 's', 't', 'a')
('r', 'e')
('a', 't', 's', 'n', 'I', 'p', 'e', 'r', 'P')