Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (84 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

String Slicing in Python: All you Need to Know

Published on Oct 23,2019 2.1K Views


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:

 

What is Slicing?

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.

 String Slicing in Python

Slicing in python is to derive a substring from the main string. Consider the below illustration of code:

 

String Slicing in Python

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.

 

Slicing a String with Negative Index

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.

 

Slicing concepts implemented in Tuples and Lists

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])

 

Slicing Concepts implemented with Negative Indices in Tuples and Lists

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.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 20th April,2024

20th April

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 22nd June,2024

22nd June

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

String Slicing in Python: All you Need to Know

edureka.co