In this tutorial, we will learn about the Python String find[] method with the help of examples.
The find[]
method returns the index of first occurrence of the substring [if found]. If not found, it returns -1.
Example
message = 'Python is a fun programming language'
# check the index of 'fun'
print[message.find['fun']]
# Output: 12
find[] Syntax
The syntax of the find[]
method is:
str.find[sub[, start[, end]] ]
find[] Parameters
The
find[]
method takes maximum of three parameters:
- sub - It is the substring to be searched in the str string.
- start and end [optional] - The range
str[start:end]
within which substring is searched.
find[] Return Value
The find[]
method returns an integer value:
- If the substring exists inside the string, it returns the index of the first occurence of the substring.
- If a substring doesn't exist inside the string, it returns -1.
Working of find[] method
Example 1: find[] With No start and end Argument
quote = 'Let it be, let it be, let it be'
# first occurance of 'let it'[case sensitive]
result = quote.find['let it']
print["Substring 'let it':", result]
# find returns -1 if substring not found
result = quote.find['small']
print["Substring 'small ':", result]
# How to use find[]
if [quote.find['be,'] != -1]:
print["Contains substring 'be,'"]
else:
print["Doesn't contain substring"]
Output
Substring 'let it': 11 Substring 'small ': -1 Contains substring 'be,'
Example 2: find[] With start and end Arguments
quote = 'Do small things with great love'
# Substring is searched in 'hings with great love'
print[quote.find['small things', 10]]
# Substring is searched in ' small things with great love'
print[quote.find['small things', 2]]
# Substring is searched in 'hings with great lov'
print[quote.find['o small ', 10, -1]]
# Substring is searched in 'll things with'
print[quote.find['things ', 6, 20]]
Output
-1 3 -1 9
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Python String find[] method returns the lowest index or first occurrence of the substring if it is found in a given string. If it is not found, then it returns -1.
Syntax: str_obj.find[sub, start, end]
Parameters:
- sub: Substring that needs to be searched in the given string.
- start [optional]: Starting position where the substring needs to be checked within the string.
- end [optional]: End position is the index of the last value for the specified range. It is excluded while checking.
Return: Returns the lowest index of the substring if it is found in a given string. If it’s not found then it returns -1.
Python String find[] method Example
Python3
word
=
'geeks for geeks'
print
[word.find[
'for'
]]
Output:
6
Note:
- If the start and end indexes are not provided then by default it takes 0 and length-1 as starting and ending indexes where ending indexes are not included in our search.
- The find[] method is similar to index[]. The only difference is find[] returns -1 if the searched string is not found and index[] throws an exception in this case.
Example 1: find[] With No start and end Argument
Python3
word
=
'geeks for geeks'
result
=
word.find[
'geeks'
]
print
[
"Substring 'geeks' found at index:"
, result]
result
=
word.find[
'for'
]
print
[
"Substring 'for ' found at index:"
, result]
if
word.find[
'pawan'
] !
=
-
1
:
print
[
"Contains given substring "
]
else
:
print
[
"Doesn't contains given substring"
]
Output:
Substring 'geeks' found at index: 0 Substring 'for ' found at index: 6 Doesn't contains given substring
Example 2: find[] With start and end Arguments
In this example, we have specified start and end arguments of Python String find[] method. So that the given substring is searched in the mentioned portion of the original string.
Python3
word
=
'geeks for geeks'
print
[word.find[
'ge'
,
2
]]
print
[word.find[
'geeks '
,
2
]]
print
[word.find[
'g'
,
4
,
10
]]
print
[word.find[
'for '
,
4
,
11
]]
Output:
10 -1 -1 6
Explanation:
- In the first statement, the output is 10 as the start value is given which is 2, so the substring is checked from the second index which is ‘eks for geeks’.
- In the second statement, the start value is given as 2 and the substring is given ‘geeks’, so the index position of ‘geeks’ is 10 but due to the last value is being excluded it will only find ‘geek’ which doesn’t matches with the original string, that is why the output is -1.
- In the third statement, start value=4, end value=10, and substring = ‘g’ is given, the index position from 4 will be checked for the given substring which is at position 10, which is being excluded as it is the end index.
- In the fourth statement, start value =4, end value=11, and substring=’for’ is given, the index position from 4 till 11 will be checked for the given substring and the specified substring is present at index 6, so the output is obtained.