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 to 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:

We can use slicing to extract a portion of a sequence (such as a string, list, or tuple) by specifying the start and stop indices.

The syntax for slicing: sequence[start:stop]

Slicing in python

Advantages of Slicing in Python Programming

  • 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.

Python code for slicing in the List

Run
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

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

Run
arr = 'PrepInsta'

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

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

#Slicing in string 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.

Python code for slicing in Tuple

Run
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')

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription