Python string find() method
Python String find() Method
Description
The find() method is used to determine whether the string str occurs within a string, or within a substring given a starting index beg and an ending index end.
Syntax
The following is the syntax of the find() method −
var.find(sub, beg = 0, end = len(string))
Parameters
- sub − Specifies the string to search for.
- beg − The starting index, which defaults to 0.
- end − The ending index, which defaults to the length of the string.
Return Value
If found, returns the index of the occurrence; otherwise, returns -1.
Example
var = "Explicit is better than implicit."
count1 = var.find('is')
print ("Original string:", var)
print ("Occurrence of 'is':", count1)
count2 = var.find('a', 0, 10)
print ("Occurrence of 'a':", count2)
Running this program will produce the following output −
Original string: Explicit is better than implicit.
Occurrence of 'is': 9
Occurrence of 'a': -1
Since “a” is not found, it returns -1.