❓ Python How-To Guides
Estimated reading: 3 minutes 421 views

Python Count Words – Simple Methods for Strings, Files, and Data

Introduction – Why Count Words?

Counting words is a common task in:

  • Text analysis
  • Search engines and NLP
  • Word frequency analytics
  • Beginner programming exercises

Python makes it easy to count total words, unique words, or word frequency, using just a few lines of code.

In this guide, you’ll learn:

  • How to count words in a string
  • Count words in a file
  • Count word frequency using collections.Counter
  • Ignore punctuation and lowercase words
  • Best practices for clean text processing

1. Count Words in a String

text = "Python is fun and Python is powerful"
words = text.split()
print("Total words:", len(words))  # Output: 7

split() separates words based on spaces by default.


2. Normalize Text Before Counting

text = "Python, python! PYTHON? Easy."
cleaned = ''.join(c.lower() if c.isalnum() or c.isspace() else ' ' for c in text)
words = cleaned.split()
print("Words:", words)

Removes punctuation, makes all words lowercase.


3. Count Words in a File

with open("sample.txt", "r") as file:
    text = file.read()

word_count = len(text.split())
print("Words in file:", word_count)

Reads entire file content and splits it into words.


4. Count Word Frequencies with collections.Counter

from collections import Counter

text = "python is fun python is easy python"
words = text.split()
freq = Counter(words)

print(freq)  # Counter({'python': 3, 'is': 2, 'fun': 1, 'easy': 1})

Tracks how many times each word appears.


Loop Through Frequencies

for word, count in freq.items():
    print(f"{word}: {count}")

5. Clean and Count (Full Example)

import string
from collections import Counter

text = "Python is great! Python is simple, and Python is powerful."

# Normalize text
translator = str.maketrans('', '', string.punctuation)
cleaned = text.translate(translator).lower()

# Count
words = cleaned.split()
freq = Counter(words)

print(freq.most_common(3))  # Top 3 words

Uses string.punctuation and translate() for efficient cleaning.


6. Count Unique Words Only

text = "one two two three three three"
words = text.split()
unique_words = set(words)
print("Unique word count:", len(unique_words))  # 3

Use set() to remove duplicates.


Best Practices

Do This Avoid This
Normalize text (lowercase + clean)Comparing mixed-case or punctuated words
Use Counter for frequencyManual nested loops
Use with when reading filesForgetting to close file handles
Handle empty inputAssuming content is always present

Summary – Recap & Next Steps

Word counting is foundational in text processing, search algorithms, and NLP tasks. Python offers flexible, efficient ways to handle it with built-ins and collections.

Key Takeaways:

  • Use split() to count words
  • Use Counter() to track frequency
  • Normalize text to lowercase and remove punctuation
  • Read files using with open() for file-based counting

Real-World Relevance:
Used in text analysis, spam filters, search engines, essay grading, and report generation.


FAQ – Python Count Words

How do I count words in a string?

Use:

len(text.split())

How do I count each word’s frequency?

Use:

from collections import Counter
Counter(text.split())

How do I ignore punctuation?

Use:

text.translate(str.maketrans('', '', string.punctuation))

How do I count words in a file?

Read the file:

with open("file.txt") as f:
    len(f.read().split())

How do I find the most frequent word?

Use:

Counter(words).most_common(1)

Share Now :
Share

Python Count Words

Or Copy Link

CONTENTS
Scroll to Top