Does Python have a string 'contains' substring method?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python does not have a dedicated contains() string method like some other languages. Instead, the normal and idiomatic way to check whether one string contains another is the in operator.
Use in for Presence Checks
If all you want to know is whether a substring exists, in is the best answer.
This returns True for "world" and False for "python".
The reason in is preferred is simple:
- it is readable
- it returns a boolean directly
- it expresses intent clearly
In Python, this is the closest equivalent to a “contains substring” method.
Use find() When You Need the Position
If you also want to know where the substring starts, use find().
find() returns:
- the starting index if found
- '
-1if not found'
That makes it useful when your next step depends on the location of the match rather than just its existence.
But if you only need a yes or no answer, in is usually cleaner than checking whether find() returned -1.
Use index() When Missing Data Should Be an Error
index() looks similar to find(), but the behavior differs when the substring is missing.
If the substring exists, you get the starting position. If it does not exist, Python raises ValueError.
That can be useful when the substring is expected to exist and failure should be explicit. It is less convenient for simple membership checks because you need exception handling if the value might be missing.
So the practical rule is:
- use
infor presence - use
find()for safe position lookup - use
index()when absence is exceptional
Use count() When Occurrence Frequency Matters
If the real question is not “does it exist” but “how many times does it appear,” use count().
This is still case-sensitive and substring-based, but it answers a different question from in.
Do not use count() > 0 just to simulate contains unless you truly need the count anyway. in is clearer for that purpose.
Case-Insensitive Checks
Python string membership is case-sensitive by default:
That returns False.
If you want a case-insensitive check, normalize both sides first:
This is a common pattern for user input, search boxes, and simple filters.
Be careful with international text and locale-sensitive matching if your application needs more than basic case folding.
Substrings vs Whole Words
Another common source of confusion is that substring search does not care about word boundaries.
That returns True, even though "cat" is not a separate word in the sentence. If you need whole-word matching, substring checks are not enough. At that point, tokenization or regular expressions may be more appropriate.
Why Python Uses in
Python leans heavily on operators that read like plain language. needle in haystack is short, expressive, and consistent with how membership works for lists, tuples, sets, and dictionaries.
So while there is no text.contains("world"), Python’s actual answer is arguably cleaner:
That is the idiom most Python developers expect to see.
Common Pitfalls
The most common pitfall is searching with find() and then writing extra logic just to convert the result into True or False. Use in instead when you only need membership.
Another mistake is forgetting that string checks are case-sensitive by default. "world" and "World" are different substrings.
A third issue is confusing substring checks with whole-word checks. "cat" in "catalog" is true, which may or may not match the intent.
Finally, some developers use index() when missing data is perfectly normal, then end up handling avoidable exceptions. In that case, find() or in is usually better.
Summary
- Python does not have a separate string
contains()method. - Use the
inoperator for the normal substring-presence check. - Use
find()when you also need the starting position without raising an exception. - Use
index()when a missing substring should be treated as an error. - Normalize with
lower()when you need a simple case-insensitive search.
Related reading
- Does Python have a ternary conditional operator?
- Does Python have an ordered set?
- Does Python have "private" variables in classes?
- Does Python optimize tail recursion?
- Does Python support multithreading? Can it speed up execution time?
- Does Python support short-circuiting?
- Does Python's time.time return the local or UTC timestamp?
- Does scikit-learn perform real multivariate regression multiple dependent variables?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.