Python Certification Training for Data Scienc ...
- 69k Enrolled Learners
- Live Class
Slicing is a cool feature to have in Python. Just like other programming languages, python also enables us to access individual characters of a string by using the technique of array-like indexing syntax. In this article, we will understand String Slicing in Python:
The main aspect of the slicing is the slice function. It allows programmers to extract information from a string of data. In this article, we can get a chance to observe many methods in which this is done. The slicing is not just restricted to strings but can be applied to tuples and lists as well.
Slicing in python is to derive a substring from the main string. Consider the below illustration of code:
print("nWelcome to Edurekan") String1=input("Enter string of your choice = " ) print("nn The output is = n") print(String1[slice(0,3)]) print("nThank you! have a nice day ")
In the below example, “ICC WORLDCUP” is a string, which is user input. The substring derived from the program is “ICC”. How did this happen? The main statement responsible for this functionality is the index of the slice function picks out the characters from index 0 (starting index) and goes up to index 2. Within the range of [0,3], the letters ICC becomes a new string and this is the output.
Another way of slicing is with regard to the negative index. This is also a good way for substring reversal. The parameters for string slicing function increases to 3. The first being the starting index from the end of the string, the second being the ending index and the third being the interval. Let us have a look.
print("nWELCOME TO EDUREKA n") String1=input("Enter string of your choice =") print("n nThe output is = n") print(String1[slice(-1,-5,-1)]) print("nThank You ! Have a nice day")
In the ‘slice’ function, the first -1 points at the last letter “M” of the string. The cursor counts backward with an interval of 1 and stops after 4 counts which leads to the output “MARG” which is the last 4 letters “GRAM” being reversed.
In the below-coded example. We see the List and tuple having elements such as the letters of EDUREKA. Each of these has the starting index of zero. The first three indices [0, 1, and 2] refer to the letters E, D, and U. Hence, the slice function pulls out the first three.
This value of 3 is stored in a variable and passes through the list and printed. When we look at the second part of the code, we see that there is an interval taken into consideration. Thus, every second index is taken of both the list and the tuple.
List1 = ['E', 'D', 'U', 'R', 'E', 'K', 'A'] Tuple1 = ('e', 'd', 'u', 'r', 'e', 'k', 'a') Obj = slice(3) print("nThe Output is n") print(List1[Obj]) Obj = slice(1, 5, 2) print("nThe output is n") print(Tuple1[Obj])
Here the functionality of the code remains the same except that the way the elements are selected is reversed. The moment we talk about negative indexing in strings, it always refers to the selecting of its string elements from the end. Let us have a look. The same thing is seen in the second half where the reversal is done but with the consideration of the intervals.
List1 = ['E', 'D', 'U', 'R', 'E', 'K', 'A'] Tuple1 = ('e', 'd', 'u', 'r', 'e', 'k', 'a') Obj = slice(-1, -5, -1) print("nThe output list isn") print(List1[Obj]) Obj = slice(-1, -6, -2) print("nThe output tuple isn") print(Tuple1[Obj])
With this, we come to the end of String Slicing in Python. To get in-depth knowledge of Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.
Got a question for us? Mention them in the comments section of “String Slicing in Python” and we will get back to you.