Open In App

Find the Index of a Substring in Python

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Working with strings is a common task in Python programming, and finding the index of a substring within a string is a frequent requirement. Whether you’re dealing with text processing, data analysis, or any other application, having the ability to locate a substring efficiently is crucial. In this article, we will explore some simple and commonly used methods to find the index of a substring in Python.

Find The Index Of A Substring In Python

Below, are the methods to Find The Index Of A Substring In Python.

Find The Index Of A Substring Using find() Function

In this example, the below code finds the index of the substring “world” within the string “Hello, world!” using the `find()` method. If the substring is found (index is not -1), it prints the index; otherwise, it indicates that the substring is not present in the text.

Python3




text = "Hello, world!"
substring = "world"
 
index = text.find(substring)
 
if index != -1:
    print(f"The substring '{substring}' is found at index {index}.")
else:
    print(f"The substring '{substring}' is not present in the text.")


Output

The substring 'world' is found at index 7.


Find The Index Of A Substring Using index() Function

In this example, below code attempts to find the index of the substring “programming” within the string “Python programming” using the `index()` method. If the substring is found, it prints the index; otherwise, it catches the `ValueError` exception .

Python3




text = "Python programming"
substring = "programming"
 
try:
    index = text.index(substring)
    print(f"The substring '{substring}' is found at index {index}.")
except ValueError:
    print(f"The substring '{substring}' is not present in the text.")


Output

The substring 'programming' is found at index 7.


Find The Index Of A Substring Using split() Function

In this example, below code finds the index of the word “Science” in the string “Data Science is fascinating” by first splitting the string into words. It prints the index if the word is found, or a message if it’s not present.

Python3




text = "Data Science is fascinating"
substring = "Science"
 
words = text.split()
try:
    index = words.index(substring)
    print(f"The substring '{substring}' is found at index {index}.")
except ValueError:
    print(f"The substring '{substring}' is not present in the text.")


Output

The substring 'Science' is found at index 1.


Find The Index Of A Substring Using regular expressions

In this example, below code uses the `re` (regular expression) module to search for the substring “expressions” within the string “Regular expressions are powerful.” If the substring is found, it prints the starting index; otherwise, it indicates that the substring is not present in the text.

Python3




import re
 
text = "Regular expressions are powerful"
substring = "expressions"
 
match = re.search(substring, text)
 
if match:
    index = match.start()
    print(f"The substring '{substring}' is found at index {index}.")
else:
    print(f"The substring '{substring}' is not present in the text.")


Output

The substring 'expressions' is found at index 8.


Conclusion

Finding the index of a substring in Python is a fundamental skill for string manipulation. The methods presented in this article provide different approaches to cater to various needs. Choose the method that best suits your specific requirements, considering factors like error handling, performance, and simplicity. Whether you opt for the built-in methods like find() and index() or employ regular expressions.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads