Python String find() Method: Syntax and 4 Examples

If you want the position of a substring in Python, the Python find method is what you need. It takes the substring and gives you back the index of the first match, or -1 when it isn’t there.

print("Austin, TX".find("TX"))
# 8

In this tutorial, I’ll cover the find() syntax plus four examples: a basic search, start and end positions with a loop, slicing an email and case-insensitive search. Outputs come from Python 3.12.5 in VS Code.

Python String find() Method Syntax

str.find(sub[, start[, end]])
ParameterRequiredDefaultDescription
subYes–Substring to look for
startNo0Where the search begins
endNolen(str)Where it stops (excluded)

Return value: an int, the lowest index of sub inside str[start:end], or -1.

You call .find() on the string itself, with nothing to import. Negative positions work like Python slices.

Note: for a plain yes or no, use in; see checking if a Python string contains a substring.

When to Use the find() Function in Python

  • Getting a word’s position before slicing
  • Checking for @ in an email
  • Walking through repeated matches
  • Parsing order logs without regex

The find function in Python fits any job where the position matters. For single characters, see how to find a character in a Python string.

Example 1: Find a Substring in a String

Let me start with US cities in one string:

cities = "Austin, Denver, Boston, Denver"

print(cities.find("Denver"))
print(cities.find("Miami"))
print(cities.find("denver"))
print(cities.find("A"))

Output:

8
-1
-1
0
Python find returning the index of Denver, -1 for Miami and a lowercase search in VS Code
find() returns the first index or -1.

How does this work? Denver starts at index 8, even though it appears twice. Miami and the lowercase denver gave -1, because find() is case-sensitive.

Pro Tip: Excel’s FIND counts from 1, but Python counts from 0.

Example 2: Use find() with start and end Positions

Here’s an order log with repeated words:

log = "ORD-1047 shipped; ORD-1052 shipped; ORD-1060 pending"

print(log.find("shipped", 10))
print(log.find("pending", 0, 30))

pos = log.find("ORD")
while pos != -1:
    print(log[pos:pos + 8])
    pos = log.find("ORD", pos + 1)

Output:

27
-1
ORD-1047
ORD-1052
ORD-1060
Python string find with start and end arguments and a while loop listing every order ID
start skips earlier matches.

How does this work? Starting at 10 skipped the first shipped and landed on 27. The 0, 30 window missed pending, and the loop restarts one past each hit until find() returns -1.

More ways: finding all occurrences of a substring and the second occurrence.

Example 3: Use find() to Split an Email Address

I use this one to clean sign-up lists:

emails = ["jordan.miller@brightline.com", "tyler.brooks",
          "emily.carter@northpeak.io"]

for email in emails:
    at = email.find("@")
    if at == -1:
        print(f"{email}: missing @")
    else:
        print(f"{email[:at]} -> {email[at + 1:]}")

Output:

jordan.miller -> brightline.com
tyler.brooks: missing @
emily.carter -> northpeak.io
Python find locating the @ sign to split user names and domains and flag a missing @
-1 flags the bad address.

How does this work? email[:at] gave the user name. tyler.brooks has no @, and the -1 check caught it.

Related: extracting a substring between two characters.

Example 4: Case-Insensitive Search with find()

Customers don’t type consistently, so lowercase first:

review = "Shipping to Seattle, WA was fast. Great SHIPPING box."

print(review.find("shipping"))
print(review.lower().find("shipping"))
print(review.lower().find("shipping", 1))

Output:

-1
0
40

How does this work? The plain search missed Shipping. After lower(), it matched at 0, and start=1 found SHIPPING at 40.

See converting a string to lowercase in Python. In pandas, Series.str.find() searches a whole column.

What Is the Difference Between find() and index() in Python?

MethodSearches fromWhen missing
find()Left-1
index()LeftValueError
rfind()Right-1

If you want a search function in Python for substrings, find() is the safe pick. Use index() when a missing value should stop the program.

Most people searching find in Python just need in. For the last match, see rfind() and the last occurrence.

Common Mistakes with Python find()

text = "Austin, TX"

if text.find("Austin"):
    print("1. found")
else:
    print("1. index 0 is falsy, so this branch runs")

print("2.", text[text.find("Ohio"):])

tests = [lambda: text.index("Ohio"),
         lambda: text.find(78701),
         lambda: ["Austin", "TX"].find("TX")]
for n, test in enumerate(tests, 3):
    try:
        test()
    except (ValueError, TypeError, AttributeError) as err:
        print(f"{n}. {type(err).__name__}: {err}")

Output:

1. index 0 is falsy, so this branch runs
2. X
3. ValueError: substring not found
4. TypeError: must be str, not int
5. AttributeError: 'list' object has no attribute 'find'
Common Python find mistakes: index 0 treated as false, slicing with -1, index ValueError, int and list errors
Five find() mistakes.
  1. Compare with != -1; 0 is falsy.
  2. Slicing with -1 returns the last character.
  3. index() raises ValueError.
  4. Convert numbers with str().
  5. Lists use list.index().

Wrapping Up the Python String find() Method

Python’s find() gives you a substring’s index or -1. Narrow it with start and end, and check for -1 before slicing.

Next, browse more Python built-in functions and methods.

Keep exploring string methods:

More Questions on Python String find() Method

How do I use find in Python?

Call text.find("word"), check the result isn’t -1, then slice.

What does .find do in Python?

It returns the first match’s starting index, or -1.

What does find() return for an empty string?

The start position: "TX-78701".find("") returned 0.

Can find() search for several words at once?

No. Loop over the words, or use re.search().

Does find() accept a regular expression?

No, only plain text. See str.find() in the Python docs.