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
- find() method in Python
- index() method in Python
- Differences Between find() and index() functions 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:
- difference between find and index in python
- what is the difference between final and immutable in java
- compare and get differences between two lists in python
- python program to generate a dictionary that contains numbers between 1 and n in the form x xx
- print all integers that arent divisible by either 2 or 3 and lie between 1 and 50 in cpp and python
- difference between vector and list in cpp
- python program to find the minimum difference between the index of two given elements present in an array