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.
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.
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.
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.
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.
This is clearer than comparing find() against -1 when the position itself is irrelevant.
Regular Expressions for Pattern-Based Search
If the target is not a literal substring, use re.search() instead.
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.
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.
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-1if missing. - Use
str.index()when missing data should raise an exception. - Pass
startandendto 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.

