There are two efficient ways to fix the “ValueError: substring not found” error in Python: using the startswith() and the find() function. The following article will describe the solutions in detail.

The ValueError: substring not found happens because you use the index function to find a value that doesn’t exist.

Example

myString = 'visit learnshareit website'

# Use the index function
print(myString.index('wel'))

Output:

Traceback (most recent call last):
File "code.py", line 4, in <module>
print(myString.index('wel'))
ValueError: substring not found

## How to solve this error?

### Use the startswith() function

Using the startswith() function is also a good solution for you to solve this problem.

Syntax:

str.startswith(str, begin=0,end=len(string))

Parameters:

• str: The string you provided wants to check.
• begin: The parameter is not required. Specify the location to start the search.
• end: The parameter is not required. Specify the location to end the search.

The startswith() function determines whether the substring occurs in an original string that you provide. If the substring is present, it will return True. And when the substring is not present, it will return False.

Example:

• Create a string.
• Illustrate the use of startswith() function with appear and non-appearance substrings.
myString = 'visit learnshareit website'

# Performs a search for a value that is not in the string
result1 = myString.startswith("wel")

print('Return value when substring is not present:', result1)

# Performs a search for a value that is in the string
result2 = myString.startswith("visit")

print('Return value when substring appears:', result2)

Output:

Return value when substring is not present: False
Return value when substring appears: True

The substring ‘wel’ does not appear in the original string, and the function returns False.

The substring ‘visit’ appears in the original string, and the function returns True.

### Use the find() function

Using the find() function is the best way to replace the index function to avoid the exception.

Syntax:

str.find(str, beg=0 end=len(string))

Parameters:

• str: The string you provided wants to check.
• begin: Specify the starting index. The default index is 0.
• end: Specify the end index. The default index is the string length (use the len() function to determine).

The find() function determines whether the substring appears in the original string. If a substring occurs, the function returns the index, otherwise it returns -1.

Note: the find() function is better than the index function in that it doesn’t throw an exception that crashes the program.

Example:

Create a string.

Illustrate the use of startswith() function with appear and non-appearance substrings.

myString = 'visit learnshareit website'

# Performs a search for a value that is not in the string
result1 = myString.find("wel")

print('Return value when substring is not present:', result1)

# Performs a search for a value that is in the string
result2 = myString.find("visit")

print('Return value when substring appears:', result2)

Output:

Return value when substring is not present: -1
Return value when substring appears: 0

Substring does not appear function returns -1.

The substring appears with an index of 0.

## Summary

Those are the two ways I recommend you to fix the ValueError: substring not found in Python. If you want to return a Boolean value, use the startswith() function, and if you want to return an index value, use the find() function depending on your search purpose. Good luck!

Maybe you are interested: