Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteFor a basic whitespace-based count, use len(text.split()). To count a text file, read it into a string and apply the same expression. This counts whitespace-separated pieces—not necessarily words according to punctuation, hyphenation, or a language’s writing rules.
Count whitespace-separated pieces in a string
Call split() without an argument, then pass the resulting list to len():
text = "Python makes this straightforward.
Here is another line."
word_count = len(text.split())
print(word_count) # 7
With no explicit separator, Python treats runs of whitespace—including spaces and newlines—as separators. Leading and trailing whitespace do not add items, and an empty or whitespace-only string produces a count of zero. Python’s str.split() documentation describes this behavior.
Count words in a text file
Read the file as text, then split the returned string. With pathlib:
#1 Best Overall
from pathlib import Path
text = Path("input.txt").read_text(encoding="utf-8")
word_count = len(text.split())
print(word_count)
Path.read_text() returns the decoded file contents as a string and closes the file after reading. The encoding must match the file; UTF-8 is shown as an explicit assumption here. See the Python Path.read_text() reference.
You can also use the built-in open() function:
with open("input.txt", encoding="utf-8") as file:
word_count = len(file.read().split())
print(word_count)
The with statement closes the file when the block ends. Both file examples count using the same whitespace-splitting rule as the string example.
Rank #2
Choose the right definition of “word”
len(text.split()) is concise when a whitespace-delimited count is what you need. It does not remove punctuation or interpret linguistic word boundaries. For example, "well-known" remains one piece, and punctuation stays attached to the surrounding text. Text with no whitespace between words remains one piece as well.
Avoid text.split(" ") for this basic count. Passing a specific separator changes the behavior: repeated spaces can produce empty items, and splitting an empty string with that separator produces [""]. If your task needs rules for punctuation, contractions, hyphenation, or languages that do not use whitespace between words, define those rules and use an appropriate tokenizer or custom logic instead.
Count a large file incrementally
Reading an entire file at once is simple, but a very large file may require substantial memory. Process it line by line and add each line’s whitespace-based count:
word_count = 0
with open("input.txt", encoding="utf-8") as file:
for line in file:
word_count += len(line.split())
print(word_count)
This avoids storing the whole decoded file in one string. It does not change the definition of a counted piece: whitespace separates items, and punctuation remains attached.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




