Check if a word is in a string in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The simplest way to check if a word is in a string in Python is the in operator: "word" in text. However, in checks for substrings, not whole words — "cat" in "concatenate" returns True. For exact whole-word matching, split the string into words or use a regular expression with word boundaries (\b). The right approach depends on whether you need substring matching, whole-word matching, or case-insensitive matching.
The in Operator (Substring Check)
in is fast (O(n) where n is the string length) and readable. Use it when substring matching is sufficient.
Case-Insensitive Check
Convert both strings to the same case:
Or use casefold() for more aggressive Unicode lowering:
Whole-Word Matching with split()
str.split() breaks the string into words by whitespace:
Limitation: split() does not strip punctuation. "Hello, world!".split() gives ["Hello,", "world!"], so "Hello" in words returns False.
Whole-Word Matching with Regex
Use \b (word boundary) for punctuation-aware whole-word matching:
\b matches at the boundary between a word character (\w) and a non-word character, so punctuation adjacent to the word does not prevent matching.
str.find() and str.index()
These return the position of the substring:
Use find() when you need the position, not just a boolean. Use in when you only need True/False.
str.count()
Count occurrences of a substring:
count() > 0 is equivalent to in but less readable and slightly slower.
str.startswith() and str.endswith()
Check if a string begins or ends with a word:
Multiple Words Check
Check if any or all words from a list are in the string:
Performance Comparison
For most use cases, the performance difference is negligible. Choose based on correctness requirements (substring vs. whole-word), not speed.
Common Pitfalls
- Substring matching when whole-word is needed:
"cat" in "concatenate"returnsTrue. If you need whole-word matching, usere.search(r"\bcat\b", text)or check againsttext.split(). - Case sensitivity:
"Hello" in "hello world"returnsFalse. Always use.lower()orre.IGNORECASEfor case-insensitive comparisons. - Punctuation with split():
"world" in "Hello, world!".split()returnsFalsebecausesplit()gives["Hello,", "world!"]. Strip punctuation first or use regex\bboundaries. - Using
isinstead ofin:"fox" is "fox"checks identity (same object), not containment. Useinfor membership testing. - Empty string always matches:
"" in "anything"returnsTrue. If the search term could be empty, check for it explicitly before usingin.
Summary
- Use
"word" in textfor simple substring checking — fast and readable - Use
"word" in text.split()for basic whole-word matching (no punctuation handling) - Use
re.search(r"\bword\b", text)for punctuation-aware whole-word matching - Use
.lower()orre.IGNORECASEfor case-insensitive checks - Use
any()andall()with generator expressions to check multiple words - Use
str.find()when you need the position of the match, not just a boolean
Related reading
- Check if all elements in a list are equal
- Check if all elements in a list are equal
- Check if any item in a list matches any item in another list
- Check if key exists and iterate the JSON array using Python
- Check if list contains element that contains a string and get that element
- Check if one list contains element from the other
- Check if something is not in a list in Python
- Check if string ends with one of the strings from a list
.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.