More Examples on String find() Method
Let’s look at some string find() method examples with programs and understand how to use the string find() method with some variations. Below are the ways by which we can use the string find method in Python:
- With No start and end Argument
- With start and end Arguments
- Total Occurrences of a Substring
- Check if the Substring Exists
1. 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.
In this example, the find() method is used on the string ‘geeks for geeks’ to locate the index of the first occurrence of the substrings ‘geeks’ and ‘for’. The method returns the starting index of the substring if found, and -1 otherwise.
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" ) |
Substring 'geeks' found at index: 0 Substring 'for ' found at index: 6 Doesn't contains given substring
2. 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 )) |
10 -1 -1 6
3. Total Occurrences of a Substring using find()
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) |
Total occurrences are: 2
4. Check if the Substring Exists using the find() Function
In this example, the code uses the find() method to check if the substring “Python” exists in the string “Python is powerful.” If the substring is found, it prints a message indicating its existence; otherwise, it prints a message stating that the substring does not exist in the text.
Python3
text = "Python is powerful." substring = "Python" if text.find(substring) ! = - 1 : print (f "The substring '{substring}' exists in the text." ) else : print (f "The substring '{substring}' does not exist in the text." ) |
The substring 'Python' exists in the text.
We have covered the definition, syntax, and different examples of the Python string find() method. This function is very useful for finding substrings within a string. It is a very simple and easy-to-use string method.
Read other Python String Methods
Read more articles related to how to find a substring in a string:
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.
Example
Python3
# initializing a string word = 'find me if you can' # using string find() method print (word.find( 'me' )) |
5