How many words are in a string
? To answer, we can develop a Python method, wordcount, that uses re.findall
to count words.
The wordcount()
method locates and counts non-whitespace characters with a special pattern. This allows us to avoid complex character testing in a loop.
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.
re.findall
method. This means "one or more non-whitespace characters."len()
built-in to count the number of elements in the resulting list. This equals the number of words in the input string.import re def wordcount(value): # Find all non-whitespace patterns. list = re.findall(r"(\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 = "Welcome, friend, to my website!" print(wordcount(value)) value = "" print(wordcount(value))10 5 0
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.
The regular expression based method for counting words does not exactly mirror all word counting implementations. But in practice the results are close.