Hi there, Python is the easiest language to learn but some of the concepts could be a little bit hard to understand but, nowadays there are so many resources to take help from and we are one of them. In this tutorial, we are going to explain Negative Indexing in Pyhton with many examples.
Table of Contents
You need nothing to learn but if you want to practice the code then you need to have a system with Python installed. You can test the given example on any version of Python.
And also, you need to know about indexing in Python programming so that you can understand negative indexing. But if you do not know much then still it is okay, we are going to explain everything.
Negative Indexing in Python List
As we know there are four built-in data types in python and the list is one of them. You can use the list data type when you want to store a list of items in a variable.
Suppose you have 5 friends and you want to store their names in a variable in python then you can do so using list data type.
Here, we are going to create a variable “MyFriends” and the data type of this variable will be “list data type”.
MyFriends = ["Jack", "Elon", "Dips", "Nancy", "Tony"]
So this is how we create a list data type variable in Python Programming.
Now, Let’s have an example of Negative indexing using the list (data type).
First of all, let’s understand the indexing-
All five names that are stored in the MyFriends variable are having an indexing position. And it starts with Zero (0).
|List Items||Positive Index Position||Negative Index Position|
Positive Index Position-
In positive indexing, the position for the first element will always be zero (0) and next will be like 1,2,3,4…. so on.
Negative Index Position-
In negative indexing, the index position for the last element will always be minus one (-1). So, the last item on the list will be the first item in negative indexing and the index position will be -1.
We hope it is clear.
Negative Indexing Example Using List-
MyFriends = ["Jack", "Elon", "Dips", "Nancy", "Tony"] print (MyFriends[-1])
Negative Indexing Example Using Slicing in Python
Slicing is a concept in programming languages like python programming, that is used by programmers to slice a piece of string and get the needed output.
Well, this tutorial is about examples so first let’s have an example and then an explanation.
We already have a variable “MyFriends” which is containing some names-
MyFriends = ["Jack", "Elon", "Dips", "Nancy", "Tony"]
Now let’s suppose, you want to call the “MyFriends” variable but you want to print the last two names that are Nancy and Tony.
So in such a case, you can slice the list and then print the last two items only using the slicing concept.
Since this article is about negative indexing and you want to see an example of negative indexing using the slicing concept in python so we will not waste time explaining all about slicing but will are directly jumping to our main topic and demonstrating it with an example.
Negative Index Slicing in Python Example-
MyFriends = ["Jack", "Elon", "Dips", "Nancy", "Tony"] lastTwoNames = (MyFriends[-2 : ]) print (lastTwoNames)
In the variable “lastTwoNames” we have assigned -2nd element which is starting element for printing and we don’t have assigned any last element after the colon. So, In the output, we will get all the elements in the list after -2nd list item but there is only one list item after -2nd item/element and that is “Tony” so we are getting the last two list items in the output.
It is not that easy to become a good programmer, you will have to practice a lot. So you need to do some experiments yourself to understand how code works.
Try these codes-
MyFriends = ["Jack", "Elon", "Dips", "Nancy", "Tony"] BestFriends = (MyFriends[-4 : -1]) print (BestFriends)
Run these codes yourself and check the output.
We would like to add that when you run this code then you will get three list items in the output and those list items will be -4th list item, -3rd list item, and -2nd list item.
The -1st list element will not be included in the output.
Negative Indexing in String in Python
You can define a string in a pair of single quotation marks (‘String’) or in a pair of double quotation marks (“String”) you can use either.
Example of String-
Name = "My Name is Jack & I am from USA"
Negative Indexing Example Using String in Python-
Let’s play with slicing a little bit more. In this example of Negative Indexing, we will slice a string using the negative index.
Let’s Slice the strings from the variable “Name”.
In the following example, we are slicing the “am” string from the “My Name is Jack & I am from USA” and this string is assigned to the variable
Name = "My Name is Jack & I am from USA" print (Name[-11 : -9])
The “a” is in the -11th position in the negative indexing and the “m” is in the 10th position. So, we are slicing the string from the -11 position to -9th position. But, here; the element of -9th position will not be included in the standard output.
What is The First Negative Index in a String
We already have given an example of the negative index in string in Python but still, if you have any confusion and you have not completely understood the topic then we would like to clear it up.
Let’s suppose, the string is the array of characters. So, The last element of the array will be the first element of the negative index.
If we talk about the list, then the last item of the list will be the first list item in negative indexing.
In the above image, you can see 4 is the last element of the array but is the first element of the negative indexing.
Suppose you want to print the first negative index of a string then you will have to write the following code-
Firstly, you will create a variable and assign a string value to it-
Name = "John"
Now print the first negative index of the string-
In this tutorial, we learned how to program using negative indexing in python. We explained how to apply negative indexing on the list and strings. Also, we have shown how to apply negative indexing on a string for slicing the part of the string. And you should note that The last element of the array will always be the first element in negative indexing.