I'm trying to find the number of occurrences of a word in a string.
word = "dog"
str1 = "the dogs barked"
I used the following to count the occurrences:
count = str1.count(word)
The issue is I want an exact match. So the count for this sentence would be 0. Is that possible?
If you're going for efficiency:
import re
count = sum(1 for _ in re.finditer(r'\b%s\b' % re.escape(word), input_string))
This doesn't need to create any intermediate lists (unlike split()
) and thus will work efficiently for large input_string
values.
It also has the benefit of working correctly with punctuation - it will properly return 1
as the count for the phrase "Mike saw a dog."
(whereas an argumentless split()
would not). It uses the \b
regex flag, which matches on word boundaries (transitions between \w
a.k.a [a-zA-Z0-9_]
and anything else).
If you need to worry about languages beyond the ASCII character set, you may need to adjust the regex to properly match non-word characters in those languages, but for many applications this would be an overcomplication, and in many other cases setting the unicode and/or locale flags for the regex would suffice.