The String find() method searches for the first occurrence of the given string and returns its index. If specified string is not found, it returns -1.
The optional arguments start and end can be used to limit the search to a particular substring of the string.
Syntax
my_string.find(sub, start, end)find() Parameters
Python String find() method parameters:
| Parameter | Condition | Description |
|---|---|---|
sub |
Required | Any string you want to search for |
start |
Optional | An index specifying where to start the search. Default value is 0. |
end |
Optional | An index specifying where to stop the search. Default is the end of the string. |
find() Return Value
Python String find() method returns an integer value:
- If the substring exists inside the string, it returns the index of the first occurrence of the substring.
- If a substring does not exist inside the string, it returns
-1.
Examples
Example 1: Find if a given String is contained in a String
If the given string is contained in the string, then find() method returns the index of the first occurrence:
my_str = 'Tom is a Developer on tutorialreference.com Development Team'
result = my_str.find('Dev')
print(result) # Output: 9output
9If the given string is not found in the string, then find() method returns -1.
my_str = 'Tom is a Developer on tutorialreference.com Development Team'
result = my_str.find('Ryan')
print(result) # Output: -1output
-1Example 2: Find if a given String is contained in a String with start and end indexes
If you want to limit the search to a substring of the given string, you can specify the start parameter and/or the end parameter.
my_str = 'Tom is a Developer on tutorialreference.com Development Team'
sub = "Dev"
result = my_str.find(sub)
print(result) # Output: 9
result = my_str.find(sub, 4)
print(result) # Output: 9
result = my_str.find(sub, 4, 6)
print(result) # Output: -1output
9
9
-1Example 3: Check if a given String exists in a String
The find() method can be used to check if a given string is contained in a string: just check the returned integer.
my_str = "Python is powerful."
sub = "Python"
if my_str.find(sub) != -1:
print(f"The substring '{sub}' exists in the text.")
else:
print("The substring does not exist in the text.")output
The substring 'Python' exists in the text.However, it is not meant to be used in this way! To check if sub is a substring or not, use the in operator.
find() method vs index() method
The find() method is identical to the index() method.
The only difference is that the index() method raises a ValueError exception, if the substring is not found.
An example to compare find() and index():
my_str = 'Tom is a Developer at ABC'
result = my_str.find('Manager')
print(result) # Output: -1
my_str = 'Tom is a Developer at ABC'
result = my_str.index('Manager') # Triggers ValueError: substring not foundoutput
-1
Traceback (most recent call last):
File "main.py", line 6, in <module>
result = my_str.index('Manager')
ValueError: substring not found