Python String find() method
Python String find() method returns the lowest index of the substring if it is found in a given string. If it is not found then it returns -1.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
str.find(sub, start, end)
- sub: It’s the substring that needs to be searched in the given string.
- start: Starting position where the sub needs to be checked within the string.
- end: Ending position where suffix needs to be checked within the string.
Note #1: If start and end indexes are not provided then by default it takes 0 and length-1 as starting and ending indexes where ending indexes are not included in our search.
Returns the lowest index of the substring if it is found in a given string. If it’s not found then it returns -1.
Note #2: The find() method is similar to index(). The only difference is find() returns -1 if the searched string is not found and index() throws an exception in this case.
Example 1: find() With No start and end Argument
Substring 'geeks' found at index: 0 Substring 'for ' found at index: 6 Doesn't contains given substring
Example 2: find() With start and end Arguments
10 -1 -1 6