In Python, the ValueError: substring not found
error occurs when you try to find a substring in a string using the index()
function, but the substring is not present in the string. In this tutorial, we’ll a deeper look at this error and understand why this error occurs and how can we fix it.
Understanding the ValueError: substring not found
error
The most common scenario in which this error occurs is when you’re trying to find the index of a substring inside a string using the built-in string index()
function. If the substring is not present in the string, this error is raised.
Let’s take a look at the index()
function.
The index()
function in Python is used to find the index of the first occurrence of a substring in a given string. It returns the index of the first character of the substring if it is found in the string, otherwise, it raises a ValueError
exception.
# create a string string = "Peter Parker" # index of the string "Parker" index = string.index("Parker") print(index)
Output:
6
In the above example, the index()
function is used to find the index of the first occurrence of the substring “Parker” in the string “Peter Parker”. The function returns the value 6, which is the starting index of the first occurrence of the substring “Parker” in the string.
If you try to find the index of a substring not present in the string using the index()
function, you’ll get an error.
# create a string string = "Peter Parker" # index of the string "Venom" index = string.index("Venom") print(index)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[5], line 4 2 string = "Peter Parker" 3 # index of the string "Venom" ----> 4 index = string.index("Venom") 5 print(index) ValueError: substring not found
In the above example, we try to find the index of the substring “Venom” in the string “Peter Parter”. Since this substring is not present in the given string, we get a `ValueError: substring not found” error.
How to fix the error?
Now that we know why this error occurs, let’s look at some ways in which we can resolve this error –
- Using the string
find()
function - Using exception handling
Using the string find()
function
The find()
function in Python is a built-in string method that also returns the index of the first occurrence of a substring within a string but instead of giving an error if the substring is not present, it returns -1
.
Let’s look at an example.
# create a string string = "Peter Parker" # index of the string "Venom" index = string.find("Venom") if index != -1: print("Substring found at index: ", index) else: print("Substring not present")
Output:
Substring not present
In the above example, we are using the find()
function to get the index of the substring “Venom” inside the string “Peter Parker”. You can see that we don’t get an error here.
Using exception handling
Alternatively, you can also use exception handling to tackle this error. Put the code where you’re trying to find the index of the substring using the index()
function inside a try
block and catch any ValueError
that may occur.
Let’s look at an example.
# create a string string = "Peter Parker" # index of the string "Venom" try: index = string.index("Venom") print("Substring found at index: ", index) except ValueError: print("Substring not found")
Output:
Substring not found
We don’t get an error here because we’re catching the ValueError
if it occurs and then printing out a message stating the substring was not found.
Conclusion
The ValueError: substring not found
error occurs when you try to find a substring in a string using the string index()
function, but the substring is not present in the string. To fix this error, you can use the string find()
function instead that returns -1
if a substring is not present instead of raising an error. You can also use a try-except block to handle the error.
You might also be interested in –