How to Count Words in Python With str.split()
Blog / TEXT PROCESSING

For a simple whitespace-separated count in Python, use len(text.split()). Calling split() without a separator treats runs of whitespace as boundaries and returns an empty list for an empty or whitespace-only string. This counts text pieces, not words under every linguistic or editorial definition.
Start with a small working example
The example below contains five whitespace-separated pieces: Hello,, world!, Python, is, and fun. The newline behaves as whitespace, so it separates world! from Python without creating an extra empty item.
Use split() rather than split(' ') when you want tabs, newlines, and repeated spaces handled together. An explicit space separator behaves differently: repeated spaces can create empty items, and a newline is not the separator you supplied.
text = "Hello, world!\nPython is fun."
count = len(text.split())
print(count) # 5
print(len(" ".split())) # 0Write a reusable helper
A small function makes the chosen rule visible wherever you use it. Name and document it as a whitespace-based count rather than claiming it identifies every natural-language word.
Try ordinary sentences alongside empty input, repeated spaces, and tabs. Those checks protect the behavior you actually chose. They do not prove that the result matches a university portal, a publishing tool, or a tokenizer designed for a particular language.
def count_words(text: str) -> int:
"""Count nonempty whitespace-separated pieces."""
return len(text.split())
assert count_words("red green\tblue") == 3
assert count_words("") == 0Read a plain-text file deliberately
For a UTF-8 plain-text file, read its contents and pass the string to the helper. A .docx or PDF file is not a plain-text file; reading its raw bytes does not extract its document prose. Use a suitable document extraction library first if that is your input.
For very large text files, count each line and sum the results to avoid loading the entire file at once. This preserves a whitespace-based rule because line boundaries are whitespace. Choose the file's actual encoding; silently ignoring decoding errors can lose text and make the total misleading.
with open("draft.txt", encoding="utf-8") as source:
total = sum(len(line.split()) for line in source)
print(total)Understand punctuation and hyphens
The string well-known guide produces two pieces with split(): well-known and guide. Punctuation-only text such as --- also produces one piece. That is why whitespace splitting is a practical baseline rather than a universal word counter.
An apostrophe remains attached to its surrounding text, and text without spaces may remain a single piece even when it contains several linguistic words. If your task needs language-aware segmentation, choose a maintained tokenizer for that language and define how numbers, symbols, and compounds should be treated.
Compare only equivalent methods
Top Word Counter uses its own word-boundary rules, so it may differ from this Python helper for punctuation or special text. A disagreement is a reason to inspect the rule and scope, not automatically a bug. For an official submission, follow the requested counter; for a script, state the counting convention alongside the result.
Frequently asked questions
What is the shortest simple Python word count?
len(text.split()) counts whitespace-separated pieces.
Does split() handle an empty string?
With no separator argument, an empty or whitespace-only string produces an empty list, so the count is zero.
Does this work identically for every language?
No. Languages and editorial rules do not all define words using whitespace. Use a suitable tokenizer when the task needs linguistic segmentation.