Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding occurrences of a word in a string in python 3

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?

like image 479
lost9123193 Avatar asked Aug 31 '25 05:08

lost9123193


2 Answers

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.

like image 184
Amber Avatar answered Sep 02 '25 18:09

Amber


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
like image 41
grc Avatar answered Sep 02 '25 19:09

grc