Explain Codes LogoExplain Codes Logo

Check if a word is in a string in Python

python
functions
regex
string-search
Alex KataevbyAlex Kataev·Feb 1, 2025
TLDR

Check if a word exists in a string with the in operator – simple and effective:

word = 'exact' phrase = 'Find the exact word in a phrase.' found = word in phrase print(found) # Prints: True. "exact"ly what you were looking for!

Ensure exact matches to avoid catching words that are merely part of other words by splitting the phrase.

Split and match full words

For cases where full word matches are essential, avoid partial matches:

words = phrase.split() # Splits "phrase" into its atomic word particles! found = word in words # Checks if the full "word" is present among the atomic particles! print(found) # True only if 'exact' is an individual word in 'phrase'

Boundary-aware regex match

To ensure matches respect word boundaries:

import re pattern = re.compile(r'\b{}\b'.format(word)) # Boundaries defined, no crossing over! found = bool(pattern.search(phrase)) print(found) # True if and only if 'exact' is a completely independent entity in 'phrase'

Advanced string search techniques

Precise word matching with regex

For fine-tuned full word searches, engage regex boundaries:

import re found = bool(re.search(r'\b{}\b'.format(re.escape(word)), phrase)) # Boundaries, we respect! print(found) # Prints: True. Bingo! 'exact' spotted in 'phrase'

Ignore case while matching

Don't discriminate based on case; match regardless of case with modified regex patterns:

found = bool(re.search(r'\b{}\b'.format(re.escape(word)), phrase, re.IGNORECASE)) # All are equal! print(found) # True for all 'exact' forms - 'Exact', eXacT, EXACT etc.

Compiled regex: Faster is better

Boost your search speed with precompiled regex patterns for repeated searches:

pattern = re.compile(r'\b{}\b'.format(re.escape(word)), re.IGNORECASE) found = bool(pattern.search(phrase)) # That was fast, wasn't it?

Context matters!

Just know, in checks for occurence anywhere in the string:

# Possibly incorrect match password = 'supersecret' found = 'secret' in password # It's not a secret anymore! Returns True

Dynamic patterns with f-strings

With Python 3.6+, use f-strings for efficient pattern formation:

pattern = re.compile(rf'\b{word}\b') # 'word' right inside the f-string; how cool is that? found = bool(pattern.search(phrase))

Python's lesser-known methods

Ever met str.find()? Another way to seek words:

index = phrase.find(word) found = index >= 0 print(found) # True if 'exact' found playing hide and seek in 'phrase'

But, remember str.find() can be slow compared to in or regex when checking presence.

Particularities of string methods

str.count() might also be useful, with a pinch of salt:

count = phrase.count(word) print(count > 0) # True if 'exact' shows up in the 'phrase' party at least once

Mind you, just like in, str.count() can count partial matches like 'exactly'.

When the "exact" isn't the only goal

At times, a partial match is just what the doctor ordered:

if 'act' in phrase: print('Partial match found!') # 'act' found moonlighting within 'exact'

Use this with caution, keeping an eye out for unintended broader matches.