Python String find() Method Syntax
str_obj.find(sub, start, end)
Parameters
- sub: Substring that needs to be searched in the given string.
- start (optional): Starting position where the substring needs to be checked within the string.
- end (optional): The end position is the index of the last value for the specified range. It is excluded while checking.
Return
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:
- If the 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.
- The find() method is similar to the index(). The only difference is find() returns -1 if the searched string is not found and index() throws an exception in this case.
Python String find() method
Python String find() method returns the lowest index or first occurrence of the substring if it is found in a given string.
Example
Python3
# initializing a string word = 'find me if you can' # using string find() method print (word.find( 'me' )) |
Output
5
Contact Us