Python String find() Method
The Python String find() method checks if the string contains the substring str, and if the beg and end ranges are specified, checks if it is contained within the specified range, and if the specified range contains the specified index value, returns the starting position of the index value in the string. If it does not contain the index value, -1 is returned.
Python String find() Syntax
Syntax of the find() method.
str.find(str, beg=0, end=len(string))
Python String find() Parameters
- str – the string to be retrieved
- beg – The start index, default is 0.
- end – the end index, defaults to the length of the string.
Python String find() Return Value
Returns the start index if the string contains substrings, otherwise -1.
Python String find() Example#1
The find() method does not take the beg and end arguments.
word = 'welcome to apidemos.com'
result = word.find('apidemos')
print("Substring 'apidemos' found at index:", result)
result = word.find('to')
print("Substring 'to ' found at index:", result)
if word.find('deepinout') != -1:
print("Contains given substring ")
else:
print("Doesn't contains given substring")
Output:
Python String find() Example#2
find() method with beg and end parameters.
word = 'welcome to apidemos.com'
print(word.find('api', 2))
print(word.find('apidemos', 2))
print(word.find('d', 4, 10))
print(word.find('to', 4, 11))
Output: