A word is defined as a sequence of characters separated by whitespace. Punctuation is treated as part of a word and does not create new words. Leading, trailing, and multiple intermediate spaces are handled gracefully.
Parameters
Name
Type
Description
Default
text
str
The input text whose words are to be counted.
required
Returns
Name
Type
Description
int
The number of words in the input text.
Raises
Name
Type
Description
TypeError
If the input text is not a string.
Examples
>>> word_count("Hello world")2>>> word_count(" This is a test ")4>>> word_count("")0