The find Method in Python: Substring Search
This method searches for a substring within a string and returns the smallest index where the specified substring begins in the original string.
find(sub[, start[, end]])
sub-- substring whose starting index must be determinedstart=0-- starting index of the slice in the original string where the substring must be foundend=None-- ending index of the slice in the original string where the substring must be found
If the substring is not found, it returns −1.
my_str = 'barbarian'
my_str.find('bar') # 0
my_str.find('bar', 1) # 3
my_str.find('bar', 1, 2) # -1
The optional parameters start and end can take any values supported by the slicing mechanism, including negative values.
Use this method only when you need to find the starting index of a substring. To check for the presence of a substring, use the in operator:
my_str = 'barbarian'
if 'bar' in my_str: ... # True