In this tutorial, I’ll explain the working of python substring. I will also explain the behavior of python substring with multiple examples. so let’s get started.
Introduction to python substring
A substring is a portion of the string. Python provides various ways to get a substring from a string. The most common is list slicing. I will explain other substring techniques as we proceed further.
Syntax of python substring
The python list slicing works on index position. Below is the syntax of python substring index slicing.
my_string[start:stop:step]
where:
start: index of the list where slicing starts.
stop: index of the list where slicing ends.
step(optional): select the step within the range start to stop index.
Get substring from python string
Let’s assume our string is “naive_skill” and we wish to extract skill from it. Below is the code snipped for the same.
l = "naive_skill"
substr = l[6:11]
print(substr)
The above codes give skill as output. The starting index is 6 and the stop index is 11.
Python substring examples
In this session, we will understand the working of python substring with multiple examples.
Python substring after a specific character
let’s assume our string is “visit naive skill to learn awesome technologies” and we wish to get the substring appears after the skill.
l = "visit naive skill to learn awesome technologies"
l.split("skill",1)[1]
we have to use the split function to split the data on the string “skill“.(“skill”,1) signifies spilling the data on 1st occurrence of the skill.
Python substring index
In this session, we will understand the python index method with the help of an example. The python index() method is used to get the index of a substring. The index method returns the lowest index in the string where the substring is found.
suppose our string is “visit naive skill to learn technologies” and we want to know the index of “learn“.
l = "visit naive skill to learn technologies"
l.index("learn")
since the lowest index was 21 so we get 21 as output.
Python string contains a substring
In some cases, a user needs to know whether a particular substring is present inside the string. This can be easily checked using the simple if condition.
l = "visit naive skill to learn technologies"
if "skill" in l:
print("found")
else:
print("not found")
Using the above code we can verify if the substring skill is present in string l.Since skill is present in l, so we get found as an output
Python substring extract a number
python provides the isdigit() function to check the presence of a digit inside a string. The isdigit() function returns true if the string contains an integer else returns false.
str = "naive4skill"
digit=""
for i in str:
if i.isdigit():
digit = digit + i
print(digit)
The above code returns 4 as the digit
Python substring regex
Python also provides the facility to extract the substring using regex. The regex should be used in places where substring can be extracted using the index method.
To use the regex a user needs to import the re module. Let’s say we need to extract the first occurrence of digit/digits from the string str.
import re
str = "789Naive456skill123"
m = re.search(r'\d+', str)
print(m.group())
The above codes give 789 as output because this is the first number that appears in the string str
If a user wishes to get all the occurrences of digit/digits in string str then he has to use the findall() method.
import re
str = "789Naive456skill123"
re.findall(r'\d+', str)
The above code returns all the numbers present in str.
Python substring between two characters
Using python programming, a user can also extract a string present between two substrings. Let’s say we need to get the string between two substrings s1 and s2.
l = "visit naive skill to learn awesome technologies"
## substrings
s1 = "skill"
s2 = "technologies"
## get index of substring
idx1 = l.index(s1)
idx2 = l.index(s2)
## getting elements in between
starting_index = idx1 + len(s1) + 1
end_index = idx2
string = l[starting_index:end_index]
print(string)
The above code prints the string present between skill and technologies which is to learn awesome.
Conclusion
I hope you have liked this tutorial on python substring. Please reach out to me if you need further input.