How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?

HossBender picture HossBender · Oct 16, 2013 · Viewed 191.5k times · Source

How would I go about counting the words in a sentence? I'm using Python.

For example, I might have the string:

string = "I     am having  a   very  nice  23!@$      day. "

That would be 7 words. I'm having trouble with the random amount of spaces after/before each word as well as when numbers or symbols are involved.

Answer

arshajii picture arshajii · Oct 16, 2013

str.split() without any arguments splits on runs of whitespace characters:

>>> s = 'I am having a very nice day.'
>>> 
>>> len(s.split())
7

From the linked documentation:

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.