Find() Function in Python – Find a position of substring

In this tutorial we will be learning how to find the position of the substring with the help of Find() function in python with an example. If the substring found then starting position is returned. If substring is not found then -1 is returned.

Syntax of Find() Function in Python:

str.find(str, beg=0, end=len(string))

str – This specifies the string to be searched.

beg – This is the starting index, by default its 0.

end – This is the ending index, by default its equal to the length of the string.

 

Example of Find() function in Python:

# find function in python

str1 = "this is beautiful earth!!";
print str1.find("this")
print str1.find("is")

# find function in python with beg and end
print str1.find("is",3,len(str1))
  • find(“this”) returns the starting position of “this” i.e 0
  • find(“is”) returns the starting position of “is” i.e. 2
  • find(“is”,3,len(str1)) returns the starting position of “is” after starting position 3 i.e.  5

so the output will be

0
2
5

 

previous Find() Function in Python                                                                                                                next Find() Function in Python