find() method in Python
The find() method is used to find the index of first occurence of a substring from the given string. if a substring is not found, it retruns -1
Syntax
string.find(substring, start, end)
Parameters
- substring - (Required) The substring to find in given string.
- start - (Optional) The index to start the search.
- end - (Optional) The index to end the search.
The following example shows, how works the find() method.
Example
txt = "This is sample text" print(txt.find("sample")) # returns 8 print(txt.find("is")) #returns 2 print(txt.find("is",4)) #returns 5 print(txt.find("was",4)) #returns -1
Output
8 2 5 -1