Difference between find() and index() in Python

Difference between find() and index() in Python

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

In this article, we’ll look at the differences between Python’s find() and index() functions. Both of these string approaches are fairly similar, with only a few exceptions. Before we go into the changes, let’s look at the find() and index() functions in Python.

Difference between find() and index() in Python

1)find() method in Python

find() is used to detect the place in a string where a substring is first found. Find(), in other words, returns the index of the first occurrence of a substring in a string. find() takes three parameters: the substring to be searched, the index of start, and the index of the end (the substring is searched between the start and end indexes of the string), the indexes of start and end being optional.

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

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

Below is the implementation:

gvnstring = 'hello this is btechgeeks python'
print(gvnstring.find('this'))
print(gvnstring.find('is', 1, 8))
print(gvnstring.find('this', 5))
res = gvnstring.find('online')
if (res != -1):
    print('The string [online] is found in given string [', gvnstring, ']')
else:
    print('The string [online] is not found in given string [', gvnstring, ']')

Output:

6
-1
6
The string [online] is not found in given string [ hello this is btechgeeks python ]

2)index() method in Python

The index() method, like find(), finds the position in a string where a substring is first found. Similarly, index() accepts three parameters: the substring to be searched, the index of start, and the index of the end (the substring is searched between the start and end indexes of the string), the indexes of start and end being optional.

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

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

Below is the implementation:

gvnstring = 'hello this is btechgeeks python'
print(gvnstring.index('this'))
print(gvnstring.index('is', 1, 20))
print(gvnstring.index('this', 5))
res = gvnstring.index('online')
if (res != -1):
    print('The string [online] is found in given string [', gvnstring, ']')
else:
    print('The string [online] is not found in given string [', gvnstring, ']')

Output:

6
8
6
Traceback (most recent call last):
File "./prog.py", line 5, in <module>
ValueError: substring not found

3)Differences Between find() and index() functions in Python

Find() gives -1 if a substring is not found in a string, whereas index() throws a ValueError exception.
As a result, find() can be used in conditional statements (if, if-else, if-elif) that run statement blocks depending on the existence of a substring in a string. However, the index() method cannot be used in conditional statements because it will generate an error.
find() can only be used with strings, but index() can be used with lists, tuples, and strings.
Related Programs: