๐ข 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 frequency | Manual nested loops |
Use with when reading files | Forgetting to close file handles |
Handle empty input | Assuming 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 :