What is an elegant way to look for a string within another string in Python, but only if the substring is within whole words, not part of a word?
Perhaps an example
One approach using the re, or regex, module that should accomplish this task is:
re
import re string1 = "pizza pony" string2 = "who knows what a pizza pony is?" search_result = re.search(r'\b' + string1 + '\W', string2) print(search_result.group())