What is slicing and indexing in python?
To understand string slicing and indexing let’s first understand what is the definition of string? So basically the String is a sequence of characters which consists of letters, numbers, symbols and white spaces. Now since strings is a sequence. In python we have a provision, using which we can access and work with each and every part of string sequence.
In technical words this accessing and working with the strings is known as string indexing and string slicing.
String Indexing in python:
String indexing means, we assign a unique number to each character of the string. Using this index numbers we can access any character of the string.
Basically we can index the string in two ways:
- Positive string indexing
- Negative string indexing
Positive string indexing:
To understand the positive way of string indexing, Let’s see one sample string “Oh My God!”. This string contains white spaces and one special character at the end of the string that is exclamation mark.
If we index this string with positive numbers it will look like as shown in image below.
The numbers written below each character of the string represents its index number. Now you can see that, First character of string “O” has index number zero “0” and the end character of string which is an exclamation mark has its index number 9. You may have also noticed that, There are white spaces in the string which has also got index number assigned i.e. number 2 and 5 are the index numbers of white spaces in string..
In python using these index numbers it is very easy to access any character.
In above example we have indexed the string from starting of the string till its end. We assigned zero index number to the starting character (“O”) and incremented the index numbers till the end of string (index number 9 assigned to exclamation mark). In python this way of indexing is known as positive way of string indexing.
Now let’s see, what is the negative way of string indexing?
Negative string indexing:
Before starting let’s first see one problem statement about,Why do we need to index the string with negative numbers?
Suppose we are having a very long string and we want to access the few of the ending characters of it. So if we go by the above mentioned method of indexing (i.e Positive number indexing), then we have to index each character of string starting from its first character till its end. Now since we are only interested in accessing the end portion of string, it will be very time consuming and unnecessary work of indexing initial part of string.
To solve this issue there is a better solution available in python, That is you can index the string in reverse direction as well. Which means you start indexing your string from its end to its start point.
In above image, We have started the string indexing from its end character. For the end character i.e for exclamation mark we have assigned -1 index number and our indexing ends at first character of string which is “O” and we have assigned index number -10 to it.
In short, when we are indexing the string from its end, we have to assign negative index numbers. Indexing will start with -1 and it will continue with negative numbers.
Looking after both the string indexing methods we can conclude that, In python strings can be indexed in both the direction. From its start to end OR from its end to start.
Now let’s move on further and see how we can use this indexing efficiently to serve our purpose. How to work with the strings using this index numbers.
String slicing in python:
To extract the required portion of string using index numbers there is a provision available know as string slicing.
To understand string slicing in python let’s see on simple example. Suppose from the string “Oh My God!” we want to extract its last character which is an exclamation mark “!” and print it on the screen.
In above image I have extracted the end character of string and printed it on the screen. Following are the steps I have performed;
- Created a new variable named my_string to store the string “Oh My God!”.
- Now I have written print command and provided the string variable (my_string) as an input to it. While giving the input I have not only given the string variable name, but in the square bracket I have also given the exact index number of exclamation mark.
Since exact index number is given to the print command. It will be very easy for the interpreter to understand what exactly we want to print.
So that was quite easy task to access a single character of string and print it. But now I want to access multiple characters of the string. Let’s see how to do that,
Range slicing in python:
To access the specific portion of string we have to specify the slice range. This slicing range needs to be provided in above mentioned format. Here x is the index number from where the string slicing will start and (y-1) is the index number where slicing will end. The character at y index number will not be involved in slicing. Now let’s see few examples how we can use string slicing.
Suppose from string “Oh My God!” we want to print the word “Oh”.
In above example we have specified the string slicing starting index number 0 and end number as 2.
Few more examples:
- Print all characters present before index number 5.
- Print all the characters present after index number 5.
So guys that was all about string slicing and indexing in python. If you are having any queries please let us know.