The substring is a part of the longer string. Finding the index of the substring is the time-consuming task if you don’t know how to do so. Therefore in this tutorial you will know the various methods to find the index of a substring in python.
Uses of Substring in Python
There are many uses of a substring in python. Below are the one of thems.
Searching and Matching
You can use the substring to find the patterns in the string and search the particular string. Thus useful for tasks such as data validation, parsing text, or extracting relevant information.
Data manipulation
You can also manipulate the sting using the substring. Suppose you want to remove or replace a subsring then how you will do so. To remove or replace you have to use the substring.
Data analysis
You can use the substring to perform data analysis. For example finding pattern.Thus helpful for tasks like sentiment analysis, keyword extraction, or text classification.
Formatting and display
Sometimes the longer string doesnot fit within specific constraints. Thus you have to extract some part of the string to fit. Therefore you have to use substrings to fit withing the constrainet.
Methods on How to Find Index of a Substring in Python
Lets know all the methods you will use to find the index of a substring in python.
Method 1: Using the find() function
This function will returns the index of the substring if it is found in the input string. Otherwise returns -1 that tells the substring is not found in the longer string.
Below is the lines of code to find the index of the susbtring.
string = "Data Science Learner"
substring = "Learner"
index = string.find(substring)
print(index)
Output

Method 2: Using the index() function
You can also use the index() function to get the index of the substring. This method is similar to the find() function but it can raise the valuerrror substring is not found if the substring is not found.
Run the below lines of code to get the index.
string = "Data Science Learner"
substring = "Learner"
index = string.index(substring)
print(index)
Output

Method 3: Find the Index of a Substring in Python using the rfind() function
The next method to find the index of substring is the use of the rfind() function. It is also similar to the find() function but it finds the index from the right end of the string.
Run the below lines of code the get the index.
string = "Data Science Learner"
substring = "Learner"
index = string.rfind(substring)
print(index)
Output
13
Method 4: Using the rindex() function
The method is similar to the index() function. But it will find the index from the right side of the string. It also raises ValueError substring not found if the substring is not found.
Use the below lines of code to find the index.
string = "Data Science Learner"
substring = "Learner"
index = string.rindex(substring)
print(index)
Output
13
Method 5: Using the regular module
You can also use the regular expression to find the index of the substring. Here you will use the re.search() function to search the substring then find the index using the match,start() function.
Run the below lines of code find the index.
import re
string = "Data Science Learner"
substring = "Learner"
match = re.search(substring, string)
if match:
index = match.start()
print(index)
Output
13
Conclusion
Getting the index of the substring is very useful if you want to find the pattern in the string or text. The above are methods to get the index of the substring. You can use any method as you want.
I hope you have liked this tutorial. If you have any queries then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.