Difference between find() and index() in Python

Hey, guys today we are going to learn the difference between find() and index() in Python. Both these string methods are very similar but have few differences. Before starting with the differences let’s discuss find() and index() methods in Python.

find() method in Python

find() is used to determine the position at which a substring is first found in a string. In other words, find() returns the index of the first occurrence of a substring in a string. find() takes 3 parameters: substring that is to be searched, index of start and index of the end(The substring is searched between the start and end indexes of the string) out of which indexes of start and end are optional.

string.find('substring',start,end)
string.find('substring',start)
string.find('substring')

If the substring is not found in a string find() returns -1.

string = 'Hello How can I help you ?'
print(string.find('can'))
print(string.find('I',10,15))
print(string.find('you',15))
a=string.find('good day')
if a!=-1:
    print('The string contains "good day"')
else:
    print('The string does not contain "good day"')

Output:

10                                                                                                                                                   

14                                                                                                                                                   

21                                                                                                                                                   

The string does not contain "good day"

index() method in Python

Just like find(), index() method determines the position at which a substring is first found in a string. In the same way index() takes 3 parameters: substring that is to be searched, index of start and index of the end(The substring is searched between the start and end indexes of the string) out of which indexes of start and end are optional.

string.index('substring',start,end)
string.index('substring',start) 
string.index('substring')

If the substring is not found in a string index() raises ValueError exception.

string = 'Hello How can I help you ?'
print(string.index('can'))
print(string.index('I',10,15))
print(string.index('you',15))
a=string.index('good day')
if a!=-1:
    print('The string contains "good day"')
else:
    print('The string does not contain "good day"')

Output:

10                                                                                                                                                     

14                                                                                                                                                     

21                                                                                                                                                     

Traceback (most recent call last):                                                                                                                     

  File "main.py", line 5, in <module>                                                                                                                  

    a=string.index('good day')                                                                                                                         

ValueError: substring not found

Difference between find() and index() in Python

  • If a substring is not found in a string find() returns -1 whereas index() raises ValueError exception.
  • As a result, find() can be used in conditional statements(if,if-else,if-elif) which run statement block on the basis of the presence of a substring in a string. However index() method can’t be used in conditional statements as it will raise an error.
  • find() can be used only with strings whereas index() can be applied to lists, tuples along with strings.

Also, refer:

Leave a Reply

Your email address will not be published. Required fields are marked *