Home
Map
Word CountCount the number of words in a string. Implement a regular expression word-counting method.
Python
This page was last reviewed on Sep 2, 2023.
Word count. How many words are in a string? To answer, we can develop a Python method, wordcount, that uses re.findall to count words.
Python method info. The wordcount() method locates and counts non-whitespace characters with a special pattern. This allows us to avoid complex character testing in a loop.
re.match
Example. The re.findall method is the most important part of this solution. It finds all matches within a string. When we count them, we can count matching patterns.
Note We specify the pattern \S+ in the re.findall method. This means "one or more non-whitespace characters."
Note 2 We use the len() built-in to count the number of elements in the resulting list. This equals the number of words in the input string.
len
import re def wordcount(value): # Find all non-whitespace patterns. list = re.findall("(\S+)", value) # Return length of resulting list. return len(list) value = "To be or not to be, that is the question." print(wordcount(value)) value = "Stately, plump Buck Mulligan came from the stairhead" print(wordcount(value)) value = "" print(wordcount(value))
10 8 0
Method results. I verified that the method counted correctly the number of words in both (trivial) examples. On more complex samples, such as ones involving markup, results may be less accurate.
So The example method does not count "word endings" but rather the words themselves.
A summary. The regular expression based method for counting words does not exactly mirror all word counting implementations. But in practice the results are close.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 2, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.