Finding occurrences of a word in a string in python 3
Use a list comprehension:
>>> word = "dog"
>>> str1 = "the dogs barked"
>>> sum(i == word for word in str1.split())
0
>>> word = 'dog'
>>> str1 = 'the dog barked'
>>> sum(i == word for word in str1.split())
1
split()
returns a list of all the words in a sentence. Then we use a list comprehension to count how many times the word appears in a sentence.
You can use str.split()
to convert the sentence to a list of words:
a = 'the dogs barked'.split()
This will create the list:
['the', 'dogs', 'barked']
You can then count the number of exact occurrences using list.count()
:
a.count('dog') # 0
a.count('dogs') # 1
If it needs to work with punctuation, you can use regular expressions. For example:
import re
a = re.split(r'\W', 'the dogs barked.')
a.count('dogs') # 1
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.