Python String Negative Indexing
This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing.
What is an Index?
An index is a numeric value, which is assigned to individual characters of the iterables. An index is used when the user wants to access individual characters of the iterables. For example, take a string “loyd" containing four characters. Therefore, it contains four indexes that range from zero to three. For any iterable, the numeric values of the indexes range from zero to "n-1”, where n is the number of characters presented in the string.
Usage of Slicing in Python
In order to print the desired part of an iterable, we make use of a quality of python, which is known as “ Slicing “. It selects a start and an end index given by the user and prints the characters of the iterable using the concatenation method.
Syntax of Slicing
inputStr[ start: end ]
1 Example: Indexing by Slicing
# considering a variable and assigning a string to it
inputStr = " Hello "
# printing the letters of the string in reverse order
print(inputStr[ 1:3])
Output
Hel
2 Example: Indexing by Slicing
# considering a variable and assigning a string to it
inputStr = " Hello "
# printing the letters of the string in reverse order
print(inputStr[ 2:0])
Output
leH
3 Example: Indexing by Slicing
# considering a variable and assigning a string to it
inputStr = " Welcome to my world "
# printing the letters of the string in reverse order
print(inputStr[ 1: 20])
Output
Welcome to my world
What is Negative Indexing
Python has an in-built feature called “negative indexing “, which is used to select and print the desired part of an iterable in reverse order. Hence, negative indexing is also called " reverse indexing”. The numerical values for negative indexing do not start with zero, and they start with " -1 ".
In a string, the last character of the string is assigned with the value " -1 ". The numerical values for negative indexing range from “ -1” to “-n ”, where n is the number of characters that are present in the string. Let us consider an example of negative indexing by using the string mentioned above, " loyd ". In zero-based indexing, the first character in the string “l” is given the index “0 ", but, in negative-based indexing, the first character " l” is Assigned with the index “ -4”. Similarly, the last character “d” is given the index “ -1” in negative indexing.
Implementation of Negative Indexing
To implement negative indexing or reverse indexing in python, we make use of the “extended slicing “concept. In the extended slicing concept, the syntax of slicing is modified and added with a negative step size. This makes the slicing in reverse order (i.e., backwards). This is done with the help of step " -1”.
Syntax of Extended Slicing
inputStr[ start: end : step ]
Program 1: By using Extended Slicing
# considering a variable and assigning a string to it
inputStr = " Hello "
# printing the letters of the string in reverse order
print(inputStr[ -1: :-1])
Output
olleH
Program 2: By using Extended Slicing
# considering a variable and assigning a string to it
inputStr = " Welcome to my world "
# printing the letters of the string in reverse order
print(inputStr[ -1: :-1])
Output 2
dlrow ym ot emocleW
Program 3: By using Extended Slicing
# considering a variable and assigning a string to it
inputStr = " Congratulations !! You have learnt Negative Indexing in Python."
# printing the letters of the string in reverse order
print(inputStr[ 1::100])
Output
nohtyP ni gnixednI evitageN tnrael evah uoY !!snoitalutargnoC
Pros of Extended Slicing in Python
- The usage of reverse indexing is done faster by using extended slicing.
- The in-built method is more convenient and easier to use.
- Takes fewer lines of code, which decreases the margin of error.
Conclusion
This feature of python makes the process easier using reverse indexing while we use the for loop to print character backwards,