The Python find() method locates the index position at which the first instance of a string occurs in another string. find() returns -1 if a value cannot be found. Otherwise, it returns the starting index position of the value for which you are searching.
Finding a substring in a string is a common operation in Python. For instance, suppose you have a string containing a list of your favorite book titles. You may want to find a certain book in that list, and return the position where that book appears in the list.
That’s where the Python string find() method comes in. The find() method allows you to search for a substring in Python. This method returns the index position of the first occurrence of that substring in the string.
This tutorial will discuss, with examples, the basics of string indexing in Python. We’ll then use the Python find() method to find a substring within a larger string.
Python Indexing: A Refresher
A Python string is a sequence that contains one or more individual characters. Strings can contain letters, white spaces, symbols, or numbers.
Consider the following string:
company = "Career Karma"
This string consists of 12 characters, each of which has its own position in the string. Here is the index breakdown for this string:
C | a | r | e | e | r | K | a | r | m | a | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
Python find() Method
The Python find() method finds the first time a particular string appears in another string. If a value is not found, the find() method returns -1. find() is very similar to the Python index() method but index() raises an exception if a value is not found.
The syntax for the find() method is as follows:
string_name.find(string_to_find, start, end)
The find() method accepts three parameters:
- string_to_find: The substring whose starting index value you want to find in a string. (Required)
- start: The index position at which the search should begin. The default for this parameter is 0. (Optional)
- end: The index position at which the search should end. (Optional)
The find() method returns the lowest index at which the first occurrence of a string starts. If the substring for which you are searching cannot be found within a string, the value -1 is returned.
You could use a Python if statement to evaluate whether -1 is returned. This will let you handle any issues that arise if the value for which you are searching cannot be found.
Now we know the syntax for the find() method, let’s explore an example that showcases this method being used.
Python Find Examples
Python find() Example: Find a String
Let’s suppose we have a string that contains a list of the best-selling ice cream flavors at a local ice cream store.
This string appears as follows:
flavors = "Vanilla, Buttered Pecan, Cookies and Cream, Chocolate, Choc Chip, Strawberry"
Our data is formatted as a string (denoted by the quotation marks).
Suppose we want to find where the entry “Cookies and Cream” appears in our string. We are interested in learning more about how popular the cookies and cream flavor is. To do so, we could use the find() built-in function. Here’s the code we would use to find this entry in our string:
flavors = "Vanilla, Buttered Pecan, Cookies and Cream, Chocolate, Choc Chip, Strawberry" cookies_and_cream = flavors.find("Cookies and Cream") print(cookies_and_cream)
Our code returns: 25.
First, we declare a Python variable called “flavors”. This variable stores the list of the most popular flavors sold at a local ice cream store.
Next, we use the find() method to find the term “Cookies and Cream” in our list, and assign the value returned by the find() method to the variable “cookies_and_cream.”
Our code returned the value 25, which means that “Cookies and Cream” starts at the index position 25 in our list.
Python find() Example: Start a Search at a Particular Index
Now, suppose we want to check whether “Choc Chip” appears after “Cookies and Cream” in our list. We know that “Cookies and Cream” starts at index position 25, so we want to search for “Choc Chip” after that index position.
We could do so using the following code:
flavors = "Vanilla, Buttered Pecan, Cookies and Cream, Chocolate, Choc Chip, Strawberry" choc_chip = flavors.find("Choc Chip", 25) print(choc_chip)
Our code returns: 55. We use find() to find “Choc Chip” in our “flavors” string. We specify one parameter, 25, which states where our search should begin in the string.
Because “Choc Chip” appears after “Cookies and Cream”, a positive value was returned. This value is the index position where “Choc Chip” appears in the list.
Python find() Example: Search Between a Particular Range
Now, suppose we want to search for whether “Buttered Pecan” appears before “Cookies and Cream”. We know that “Cookies and Cream” starts at index position 25, so we don’t want to search for “Buttered Pecan” after that point. To perform this search, we could use the following code:
flavors = "Vanilla, Buttered Pecan, Cookies and Cream, Chocolate, Choc Chip, Strawberry" buttered_pecan = flavors.find("Buttered Pecan", 0, 25) print(buttered_pecan)
Our code returns: 9. In this example, we used find() to find “Buttered Pecan” in our string. We specified 0 as the position at which our search should start, and 25 as the position at which our search should end.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Because “Buttered Pecan” appears before “Cookies and Cream”, a positive value was returned with the index position of “Buttered Pecan” in our string.
Python find() vs. Python index()
Both the Python find() and Python index() methods return the index position of a value in a string.
Unlike index(), find() does not return an exception if a value is not found. Instead, the find() method returns -1. This means that you can process what happens if a value is not found without handling an error.
Conclusion
The Python find() method allows you to find a substring within a string. It is similar to the index() method, but the index() method raises an exception if a value is not found. find() returns -1 if a value is not found.
This tutorial discussed, with reference to examples, the basics of string indexing and how to use the Python find() method. Now you’re equipped with the knowledge you need to start finding substrings in a string using find() like an expert!
For guidance on top Python learning resources and courses, check out our How to Learn Python guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.