Thursday, December 26, 2024
Google search engine
HomeLanguagesPython String find() method

Python String find() method

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 Examples

Python3




word = 'geeks for geeks'
print(word.find('for'))


Output:

6

Note:

  1. 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.
  2. 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.

find() With No start and end Argument

When the find() function is called without specifying the start and end arguments, it searches for the first occurrence of the substring within the entire input string from the beginning to the end.

Python3




word = 'geeks for geeks'
 
# returns first occurrence of Substring
result = word.find('geeks')
print("Substring 'geeks' found at index:", result)
 
result = word.find('for')
print("Substring 'for ' found at index:", result)
 
# How to use find()
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

Explanation :

In the code, We are finding the word geeks in the string “geeks for geeks” with the find() function of Python. The First Find() function is returning the index of the first geeks word found in the string “geeks for geeks”, which is the index 0. The Second Find() function is returning the index of the first for word found in the string “geeks for geeks”, which is index 6. In the end, we are finding the pawan in the string “geeks for geeks”, which is not there so the find() function is returning -1 and according to the condition, the output Doesn’t contains given substring.

find() With start and end Arguments

When the find() function is called with the start and/or end arguments, it searches for the first occurrence of the substring within the specified portion of the input string, delimited by the start and/or end indices.

Python3




word = 'geeks for geeks'
 
# Substring is searched in 'eks for geeks'
print(word.find('ge', 2))
 
# Substring is searched in 'eks for geeks'
print(word.find('geeks ', 2))
 
# Substring is searched in 's for g'
print(word.find('g', 4, 10))
 
# Substring is searched in 's for g'
print(word.find('for ', 4, 11))


Output:

10
-1
-1
6

find() Total Occurrences of a Substring

find() function can be used to count the total occurrences of a word in a string.

Python3




main_string = "Hello, hello, Hello, HELLO! , hello"
sub_string = "hello"
count_er=0
start_index=0
for i in range(len(main_string)):
  j = main_string.find(sub_string,start_index)
  if(j!=-1):
    start_index = j+1
    count_er+=1
print("Total occurrences are: ", count_er)


Output :

Total occurrences are:  2

RELATED ARTICLES

Most Popular

Recent Comments