Last updated: Apr 8, 2024
Reading timeยท3 min

The Python "ValueError: substring not found" occurs when we pass a value that
doesn't exist in the string to the str.index() method.
To solve the error, use the find() method instead, e.g. my_str.find('z'),
or handle the error using a try/except block.

Here is an example of how the error occurs.
my_str = 'bobby' # โ๏ธ ValueError: substring not found idx = my_str.index('z')

The str.index() method raises a ValueError if the substring is not contained
in the string.
The substring we passed to the index method is not contained in the string
which caused the ValueError.
str.find() method to avoid getting the errorOne way to get around this is to use the str.find() method instead.
my_str = 'bobby' index = my_str.find('z') print(index) # ๐๏ธ -1 if index != -1: print(my_str[index]) else: # ๐๏ธ this runs print('The character is not contained in the string')

The str.find() method returns the index of the first occurrence of the
provided substring in the string.
The method returns -1 if the substring is not found in the string.
If you need to call the str.index() method on a string in a case-insensitive
manner, convert both strings to lowercase.
my_str = 'BOBBY' test_char = 'b' index = my_str.lower().index(test_char.lower()) print(index) # ๐๏ธ 0

We used the str.lower() method to convert the string and the character to
lowercase to perform a case-insensitive test.
The code sample basically uses the str.index() method in a case-insensitive
manner.
Alternatively, you can check if the substring is present in the string before
calling the index() method.
my_str = 'bobby' if 'z' in my_str: idx = my_str.index('z') print(idx) else: # ๐๏ธ this runs print('The substring is not contained in the string')
The in operator tests for membership.
For example, x in s evaluates to True if x is a member of s, otherwise
it evaluates to False.
my_str = 'bobby' print('b' in my_str) # ๐๏ธ True print('z' in my_str) # ๐๏ธ False
The str.index() method returns the index of the first occurrence of the provided substring in the string.
The method raises a ValueError if the substring is not found in the string.
try/except statement to handle the errorYou can also use a try/except block to handle the scenario where the substring is not found in the string.
my_str = 'bobby' try: idx = my_str.index('z') print(idx) except ValueError: # ๐๏ธ this runs print('The substring is not contained in the string')
We call the index() method on the string and if a ValueError is raised, the
except block is run.
If you want to ignore the error in the except block, use a pass statement.
my_str = 'bobby' try: idx = my_str.index('z') print(idx) except ValueError: pass
The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.
You can also use a one-liner if/else statement.
my_str = 'apple' result_1 = my_str.index('z') if 'z' in my_str else None print(result_1) # ๐๏ธ None result_2 = my_str.index('a') if 'a' in my_str else None print(result_2) # ๐๏ธ 0
If the substring is present in the string, we return the result of calling the
index() method with the substring, otherwise we return None.
if you need to check if a string starts with or ends with a substring, use the
str.startswith() and str.endswith() methods instead.
my_str = 'bobby' test_char = 'b' if my_str.startswith(test_char): # ๐๏ธ this runs print('The string starts with the substring') else: print('The string does not start with the substring')
The
str.startswith
method returns True if the string starts with the provided prefix, otherwise
the method returns False.
Here is an example that uses the endswith() method.
my_str = 'bobby' test_char = 'by' if my_str.endswith(test_char): # ๐๏ธ this runs print('The string ends with the substring') else: print('The string does not end with the substring')
The
str.endswith()
method returns True if the string ends with the provided suffix, otherwise the
method returns False.
You can learn more about the related topics by checking out the following tutorials: