fix valueerror substring not found in python

How to Fix – ValueError: substring not found

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.

fix valueerror substring not found in python

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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 –

  1. Using the string find() function
  2. 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 –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top