Python Word Count Script Example
This example shows a simple way to count words in Python.
You will build a small script that:
- takes a piece of text
- splits it into words with
split() - counts those words with
len()
This is a beginner-friendly example focused on one practical task. It does not try to do advanced text analysis.
Quick example
text = "Python makes word counting simple"
words = text.split()
count = len(words)
print("Word count:", count)
Output:
Word count: 5
This is the simplest version. It splits text by spaces and counts the resulting words.
What this example does
This script:
- shows how to count words in a short string
- uses
split()to break text into words - uses
len()to count how many words were found - targets beginners who want a small practical script
Basic word count example
Start with a plain text string, split it, and count the result.
text = "Python makes word counting simple"
words = text.split()
count = len(words)
print("Words:", words)
print("Word count:", count)
Output:
Words: ['Python', 'makes', 'word', 'counting', 'simple']
Word count: 5
This works because:
text.split()creates a list of wordslen(words)counts how many items are in that listprint()shows the result clearly
How the code works step by step
Here is the same code again:
text = "Python makes word counting simple"
words = text.split()
count = len(words)
print("Word count:", count)
Step 1: Store the text
text = "Python makes word counting simple"
The variable text stores the sentence as a string.
Step 2: Split the text into words
words = text.split()
split() separates the string on whitespace.
That means spaces, tabs, and line breaks can all act as separators.
The result here is:
['Python', 'makes', 'word', 'counting', 'simple']
Step 3: Count the words
count = len(words)
len() returns the number of items in the list.
Since the list has 5 items, the word count is 5.
If you want to learn these parts in more detail, see the reference pages for split() and len().
Reading text from user input
You can make the script interactive by asking the user to enter a sentence.
If you are new to this, see how to get user input in Python.
text = input("Enter a sentence: ")
words = text.split()
count = len(words)
print("Word count:", count)
Example run
Enter a sentence: Python is fun to learn
Word count: 5
This follows the same pattern:
- get text with
input() - split it into words
- count the words with
len()
Counting words in a text file
You can also count words in a file.
The basic idea is:
- open the file
- read all text into one string
- use
split()andlen()
If needed, see how to read a file in Python.
with open("sample.txt", "r", encoding="utf-8") as file:
text = file.read()
words = text.split()
count = len(words)
print("Word count:", count)
Example file content
Python makes word counting simple.
This is a short file.
Possible output
Word count: 9
The important part is file.read(). It reads the file content into one string so you can count the words.
Limits of the simple approach
This example is useful, but it is still a simple method.
Things to know:
- punctuation stays attached to words
- hyphenated words may not behave as expected
- multiple spaces are handled well, but special text patterns may not be
- this is for simple counting, not advanced language processing
For example:
text = "Hello, world!"
print(text.split())
print(len(text.split()))
Output:
['Hello,', 'world!']
2
This counts 2 words, which may be fine for many cases. But notice that Hello, still includes the comma, and world! still includes the exclamation mark.
Simple improvement with cleanup
A small improvement is to clean basic punctuation before splitting.
This version:
- converts text to lowercase
- removes a few common punctuation marks
- then counts the words
text = "Hello, world! Python makes word counting simple."
clean_text = text.lower()
clean_text = clean_text.replace(",", "")
clean_text = clean_text.replace(".", "")
clean_text = clean_text.replace("!", "")
words = clean_text.split()
count = len(words)
print("Clean text:", clean_text)
print("Words:", words)
print("Word count:", count)
Output:
Clean text: hello world python makes word counting simple
Words: ['hello', 'world', 'python', 'makes', 'word', 'counting', 'simple']
Word count: 7
This improves results for many common sentences.
It is still a simple approach, but it is often better than splitting the raw text directly.
Expected output
Here is a short sample sentence and its expected result:
text = "Python makes word counting simple"
words = text.split()
count = len(words)
print("Word count:", count)
Output:
Word count: 5
If your result is different, check the exact text you used.
Common mistakes
Here are some common problems beginners run into.
Using len(text) instead of len(text.split())
This counts characters, not words.
text = "Python makes word counting simple"
print(len(text)) # character count
print(len(text.split())) # word count
Forgetting parentheses in split()
This is wrong:
words = text.split
This is correct:
words = text.split()
You must call the method with parentheses.
Trying to count file words without reading the file first
This will not work the way you want:
with open("sample.txt", "r", encoding="utf-8") as file:
print(len(file.split()))
A file object does not have the text yet. You need to read the content first:
with open("sample.txt", "r", encoding="utf-8") as file:
text = file.read()
print(len(text.split()))
Expecting punctuation to be treated the same
In the simple version:
"hello""hello,"
are not exactly the same text.
That is why cleanup can improve results.
Debugging tips
If your script is not giving the result you expect, print the intermediate values.
Useful checks:
print(text)
print(text.split())
print(len(text.split()))
print(repr(text))
What these help with:
print(text)shows the original textprint(text.split())shows the exact pieces being countedprint(len(text.split()))confirms the final countprint(repr(text))helps you see hidden characters like\n
FAQ
Does split() always count words correctly?
No. It works well for simple text, but punctuation and special cases can affect the result.
Why not use len(text) for word count?
len(text) counts characters, not words. You need len(text.split()) to count word-like pieces.
Can I count words in a file with the same method?
Yes. Read the file into a string first, then use split() and len().
What happens with extra spaces?
split() handles normal extra whitespace well and usually ignores repeated spaces.