Find function in Python string

In this Python tutorial, I will show what is the find function in Python string, Its syntax, parameters, and return values. we will also see different demonstrative examples for the find() function.

What is the find() function in Python?

The find() method is an inbuilt function in Python programming language that returns the index position of the first occurrence of the specified value or substring in a string. If not found, it returns -1.

Syntax

The syntax of the find() method in Python string is: Here, the string is the original Python string in which we want to search.

string.find(value, start, end)

Parameters

The Python find() method takes maximum of three parameters: one compulsory and two optional

  • value: It is the value or substring that needs to be searched in the original Python string.
  • start(optional): Starting index number within the Python string shows where to start the search.
  • end(optional): Ending index number within the Python string where to end the search.

Note: Python is case-sensitive, so ‘california’ and ‘California’ would be considered different strings.

Return values

The find() method in Python returns an integer value:

  • If the substring exists inside the original Python string, it returns the index of the first occurrence of the substring in the Python string(always a Positive index number)
  • If the substring doesn’t exist inside the original Python string, it returns -1.

Find() function with examples

The find function in Python string has a variety of applications. Here are some practical examples that demonstrate its usage:

Example-1: Searching a substring in a Python String

This is the most basic way to use the find() method in Python string to search a substring.

For instance, we have stored the names of different singers voted by peoples in an event held in the US in the form of a Python string with commas. And we have to search if anyone has voted for ‘Beyonce’ in the event:

singers = "Taylor Swift, Lady Gaga, Selena Gomez, Katy Perry, Beyonce"

Voted = singers.find('Beyonce')
print('Beyonce is present at:', Voted)

The output is: The find function will return the position where the Beyonce is present in the original Python String.

Beyonce is present at: 51
Find() function in Python string to search a substring

This way we can use the find() function in Python string to search a substring.

Example-2: Find() method with Start and End Parameters in Python string

Here, we will use the start and end parameters to search a specific substring within the range in a Python string find() function.

In this scenario, we have a Python string named ‘presidents’ that contains the names of several famous presidents of the United States. We use the string find() method to look for ‘Abraham Lincoln’ within a specific segment of the string: from the 30th character to the 80th character.

presidents = "George Washington, Thomas Jefferson, Abraham Lincoln, Franklin Roosevelt, John Kennedy, Barack Obama"
start = 30
end = 80

position = presidents.find('Abraham Lincoln', start, end)
print('The Abraham Lincoln is present at: ', position, 'Position')

The output is:

The Abraham Lincoln is present at:  37 Position
Python find method in string with start and end parameter

This example illustrates how we can use the start and end parameters in the find() method to limit our search to a particular section of the string in Python. This can be particularly useful when dealing with very large strings in Python or when we know the general location of the information we’re looking for.

Example-3: Searching the substring which is not present using the find() method in Python string

Let’s check what happens when we give the find method in Python a non-existing substring as a parameter.

In this scenario, we have a string named ‘landmarks’ that contains the names of several famous landmarks in the United States. We use the Python string find() method to look for the ‘Golden Gate Bridge’ within the string.

landmarks = "Grand Canyon, Statue of Liberty, Mount Rushmore, Yellowstone National Park."

position = landmarks.find('Golden Gate Bridge')
print(position)

The output is:

-1
Python string find() function to find a non-existing substring

The ‘Golden Gate Bridge’ did not appear in the string. So, Python find() method has returned -1. This example illustrates how we can use the find() method to search for a specific substring within a larger string whether it is there or not.

Conclusion

Throughout this article, we’ve explored the power and utility of the find() function in Python strings, making use of a variety of scenarios. As one of Python’s built-in string methods, the find() Method offers a versatile approach to locate the presence and position of a substring within a larger string. We have seen what is its syntax, parameters and return values.

You may also like to read: