Python String Slicing
String slicing simply means that we are getting a range of characters with the specified indexes. Here, we are going to provide the starting index and the ending index (note that the ending index is not included). This is like we are saying that from the given string, we want another string, which has the characters from the starting index to the ending index.
For example, Let’s consider the string ‘GyaniPandit’, and Let’s say that we want another string from the given string, which consists of the characters from index 1 to index 5(actually index 5 is not included, so we are going to get the characters till the index 4). So, the new string would be ‘Yani’.
So, first of all, Let’s have a look at a program, which demonstrates the same thing.
Python String Slicing
Notice how are we doing the string slicing. We are just using the same regular square brackets, which are used to access the individual characters in the string. In there, you can see two input numbers, which are the starting index, and the ending index respectively, separated by the colon. Here, the ending index is not included. So, the last character in another string would be the 4th index character, and the starting character would be the index 1 character. If you run the program, the output would be simply ‘Yani’. Here, we are assigning the new sliced string to another reference variable.
One very important thing to note is that the original string is not going to be affected due to this slicing. Instead, a new string would be returned, which would be sliced, and will have the required characters.
So, from the above example, you can simply try to print the original string, and it would print as it is.
Now, Let’s go beyond this, and try some other things related to the slicing of strings.
You might be familiar now, that we are giving two inputs while slicing, and they are separated by a colon. The first value is the starting index, and another value is the ending index. But if some index is missing, like if the starting index or the ending index is missing? In that case, what output are we going to get? Are we going to get an error because the indexes are not given? Well, Let’s explore it now.
Let’s consider that the starting index is missing. So, there is going to be no input value before the colon, and we are going to have the ending index after the colon. Have a look at the program, which demonstrates the same thing –
As you can see, while we are slicing above, the starting index is missing. So, in the above program, are we going to get an error? Well, the answer is simply NO. We are not going to get an error, instead, if we are not giving the starting index, it will consider the 0 as the starting index. So, we are going to get the new string with the characters from the start to the 5th index (the 5th index is not included, so the last character would be the 4th index character).
So, if we try to run the above program, the output would be ‘Gyani’.
Similar to this, if the ending index is missing, again we are not going to get into errors, but instead, the string would be sliced till the end of the string. Have a look at the program, which tries to demonstrate the same thing.
This time, the new string that is going to be sliced, is going to have the characters from index 5 to the last of the string. So, if we run the program, the output is going to be ‘Pandit’. This is very easy to understand.
Now, Let’s consider that both indexes are missing now. In such a case, what are we going to get? Well, again if the indexes are missing, the defaults will be considered, which means the string is going to be sliced from the start to the end of the string. So, we are going to get a new string, which has all the characters.
Now, in the above program, we can say that both indexes are missing. This time, we are going to get the new string, which is going to be sliced from the start to the end of the string. So, if we try to run the above program, we are going to get the whole string as the output. You can try running the above program and confirm the output.
So, we can make use of string slicing, whenever we need to get another string, from our given string, which contains a certain range of characters from the given string. We are just required to use the square brackets, and we have to provide the starting index, and ending indexes separated by the colon, and if we are not specifying the values, the defaults would be considered.
So,
print(str1[:]) → prints whole string str1 as nothing was specified.
print(str1[5:]) → prints from the 5th index to the end.
print(str1[:5]) → prints from the start to the 5th index (but excluding the 5th index).
Also, if we are trying to give some ending index, which is not even in the range of the string, are we going to get into errors? The simple answer is NO. In the above program, you can try giving the ending index as something which is greater than the length of the string, like 29. So, this time as well, we are not going to get into errors, but we would get the string up to the end of the string. So, Let’s have a look at another program, which tries to demonstrate the same –
As you can see, we have given the starting index, as zero, and the ending index is actually something out of the range of the string, but still, we are not going to get any error, but we are going to get the string to end.
But if you give starting index which is greater than the length of the string, then it would print nothing because it has nothing to print.
Now, Let’s say that you want to access the last element of the string, so for this, you would need to have the length of the string, since the last element index would be the length of the string minus 1, but what if you do not want to calculate the length, but still get the last character from the string? Well, this can be done easily. We just need to use something called negative indexing. This simply means that when we are giving the index as -1, then this would be the last character in the string, just like if we give 0 as an index, it is the first character in the string. So, going through other numbers like -2, -3, and so on, would get you 2nd and third characters from the last, and so on. In short, when we are using negative indexing, the characters are accessed from the last of the string. Have a look at the example, so that we can get more clarity –
As you can see in the above program, we are simply having a string, and then we are trying to access the characters using negative indexing. The index -1, means the last character. If you have a look at the output, it is simply going to print the last character, the last second, and the last third characters.
So, you can use negative indexing as well.