Python String find() Function
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.
The find()
method should be used only if you need to know the position of string to be searched.
To check if sub is a substring or not, use the in
operator.
If you want to get the last occurrence of the specified string, then you have to use the rfind()
method.
Syntax
my_string.find(sub, start, end)
find() Parameters
Python String find()
function 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()
function 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: 9
output
9
If 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: -1
output
-1
Example 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"
# find()
result = my_str.find(sub)
print(result) # Output: 9
# find() after 4th index
result = my_str.find(sub, 4)
print(result) # Output: 9
# find() between 4th and 6th index
result = my_str.find(sub, 4, 6)
print(result) # Output: -1
output
9
9
-1
Example 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 found
output
-1
Traceback (most recent call last):
File "main.py", line 6, in <module>
result = my_str.index('Manager')
ValueError: substring not found