Slicing with Negative Numbers in Python

Negative slicing in python

Slicing in Python with Negative Indexes

Slicing with Negative Numbers in Python is a subpart of slicing in python. Slicing is used to iterate through the sequential or iterable data-type in the python programming language.

Slicing with Negative Numbers in Python

In this Section, we will look at the slicing on some iterable or sequential data-types like:

  • List
  • String
  • Tuple

When we use a negative number as the start or stop index in a slice, it counts from the end of the sequence with -1 representing the last element, -2 representing the second to last element and so on.

Slicing with Negative Numbers in Python

Python code for Slicing with Negative Indexes

Slicing in String with Negative Indexes

The string is a data-type belongs to sequence data-type category. Slicing in the string is used to iterate through the elements. We have negative as well as negative indexes of any iterable data-type. In the below python code we will be iterating through negative indexes of the string elements.

Run
#Initialize the String
String = 'PrepInsta'

#Slicing using 1 negative index
arr = String[-2:]
print(arr)

#Slicing using 1 negative index
arr = String[:-3]
print(arr)

#Slicing using 3rd negative index
arr = String[::-1]
print(arr)

#Slicing using 2 negative indexes
arr = String[-9:-4]
print(arr)
Output:
ta
PrepIn
atsnIperP
PrepI

Slicing in List with Negative Indexes

The list is a data-type in Python programming language. Slicing is an iterable data-type and so we can perform some slicing operations on the string. Slicing is done using indexes of the iterable. Here we will iterate through the list using the negative indexes.

Run
#Initialize the String
String = ['P', 'r', 'e', 'p', 'I', 'n', 's', 't', 'a']

#Slicing using 1 negative index
arr = String[-2:]
print(arr)

#Slicing using 1 negative index
arr = String[:-3]
print(arr)

#Slicing using 3rd negative index
arr = String[::-1]
print(arr)

#Slicing using 2 negative indexes
arr = String[-9:-4]
print(arr)
Output:
['t', 'a']
['P', 'r', 'e', 'p', 'I', 'n']
['a', 't', 's', 'n', 'I', 'p', 'e', 'r', 'P']
['P', 'r', 'e', 'p', 'I']

Slicing in Tuple with Negative Indexes

Tuple data type is sequential data-type in python programming. A tuple is an immutable data-type. But we can perform many operations on the tuple. As it is an iterable data-type we can perform some slicing functions on the tuple. Let’s have a look at the python codes for slicing on the tuple.

Run
#Initialize the String
String = tuple(['P', 'r', 'e', 'p', 'I', 'n', 's', 't', 'a'])

#Slicing using 1 negative index
arr = String[-2:]
print(arr)

#Slicing using 1 negative index
arr = String[:-3]
print(arr)

#Slicing using 3rd negative index
arr = String[::-1]
print(arr)

#Slicing using 2 negative indexes
arr = String[-9:-4]
print(arr)
Output:
('t', 'a')
('P', 'r', 'e', 'p', 'I', 'n')
('a', 't', 's', 'n', 'I', 'p', 'e', 'r', 'P')
('P', 'r', 'e', 'p', 'I')

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