python
string manipulation
substring search
find method
programming tips

How can I find the first occurrence of a sub-string in a python string?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To find the first occurrence of a substring in Python, the usual tools are str.find() and str.index(). They do almost the same search, but they differ in how they report “not found.” Choosing between them depends on whether absence is normal control flow or an error condition.

Use str.find() for Safe Lookup

find() returns the lowest index where the substring starts. If the substring is missing, it returns -1.

python
1text = "the quick brown fox"
2
3print(text.find("quick"))   # 4
4print(text.find("wolf"))    # -1

This is the most convenient choice when you want to test for presence without handling exceptions.

Use str.index() When Missing Data Is an Error

index() performs the same search but raises ValueError if the substring is not present.

python
1text = "the quick brown fox"
2
3print(text.index("brown"))  # 10
4
5try:
6    print(text.index("wolf"))
7except ValueError:
8    print("substring not found")

Use index() when a missing substring should be treated as a failure, not just a negative result.

Limiting the Search Range

Both find() and index() accept optional start and end arguments. That is useful when you only want to search part of the string.

python
1text = "abc def abc def"
2
3first = text.find("abc")
4second = text.find("abc", first + 1)
5
6print(first)   # 0
7print(second)  # 8

This pattern is often easier than slicing the string manually and then adjusting indices afterward.

Case-Insensitive Searches

Python string search is case-sensitive by default. If you need a case-insensitive first match, normalize both values first.

python
1text = "Hello Python"
2needle = "python"
3
4pos = text.lower().find(needle.lower())
5print(pos)  # 6

This works well for simple text handling. If you care about locale-specific rules, the problem becomes more subtle and plain lowercasing may not be enough.

Checking Presence Without the Position

If you only need to know whether the substring exists, do not search for the index at all. Use the membership operator.

python
1text = "the quick brown fox"
2
3print("quick" in text)   # True
4print("wolf" in text)    # False

This is clearer than comparing find() against -1 when the position itself is irrelevant.

If the target is not a literal substring, use re.search() instead.

python
1import re
2
3text = "Order ID: 48291"
4match = re.search(r"\d+", text)
5
6if match:
7    print(match.start())   # 10
8    print(match.group())   # 48291

Regular expressions are more powerful, but they are also heavier and harder to read. Do not use them if a simple substring search is enough.

Finding the First Match After a Marker

Real string parsing often means “find X after Y,” not just “find X anywhere.” Use the start argument rather than chopping the string apart.

python
1text = "name=ava;status=active;status=archived"
2
3marker = text.find("status=")
4if marker != -1:
5    next_status = text.find("status=", marker + 1)
6    print(next_status)

This keeps indices in the original string, which makes downstream slicing easier.

Extracting Around the Match

Once you have the starting index, you can slice around it to extract the matched part or surrounding text.

python
1text = "path=/api/v1/users"
2prefix = "path="
3
4pos = text.find(prefix)
5if pos != -1:
6    value = text[pos + len(prefix):]
7    print(value)  # /api/v1/users

This is a common pattern in ad hoc parsing and simple protocol handling.

Performance Notes

Python’s built-in string search functions are implemented in C and are efficient for normal use. Avoid writing your own character-by-character Python loop unless you have a special matching rule that the built-ins cannot express. For very large-scale text processing, algorithm choice and data format matter more than tiny differences between find() and index().

Common Pitfalls

The most common mistake is using index() when a substring may legitimately be absent, which turns a normal case into an exception path. Another is forgetting that string search is case-sensitive by default. Developers also sometimes use regular expressions for plain fixed-string lookups, which adds complexity without benefit. Finally, if you slice the string before searching, it is easy to lose track of the correct index in the original text.

Summary

  • Use str.find() when you want the first index or -1 if missing.
  • Use str.index() when missing data should raise an exception.
  • Pass start and end to limit the search range.
  • Normalize case when you need case-insensitive matching.
  • Use re.search() only when you need pattern matching rather than fixed-substring lookup.

Course illustration
Course illustration

All Rights Reserved.