How to Validate Text and Count Words in Python

Count word frequencies, find unique and repeated words in a text using Python dictionaries and sets for beginner text validation.

Easy Python 3.9+ Aug 9, 2026 Dictionaries & sets 12 views 0 copies

Python code

22 lines
Python 3.9+
def validate_text(text):
    words = text.lower().split()
    
    word_counts = {}
    for word in words:
        cleaned = word.strip('.,!?;:"\'')
        if cleaned:
            word_counts[cleaned] = word_counts.get(cleaned, 0) + 1
    
    unique_words = set(word_counts.keys())
    repeated_words = {word for word, count in word_counts.items() if count > 1}
    
    print(f"Total words: {len(words)}")
    print(f"Unique words: {len(unique_words)}")
    print(f"Word counts: {word_counts}")
    print(f"Repeated words: {repeated_words}")
    
    return word_counts

if __name__ == "__main__":
    sample_text = "Python is fun. Python is powerful and fun!"
    validate_text(sample_text)

Output

stdout
Total words: 7
Unique words: 5
Word counts: {'python': 2, 'is': 2, 'fun': 1, 'powerful': 1, 'and': 1}
Repeated words: {'python', 'is'}

How it works

The text.lower().split() normalizes case and splits text into a list of words. The dictionary word_counts stores each cleaned word as a key and its frequency as a value, using .get() to safely increment counts. A set from the dictionary keys gives unique words, while a set comprehension filters words with counts greater than one to find repeated words. This demonstrates efficient lookups with dictionaries and uniqueness checks with sets in a single, readable function.

Common mistakes

  • Forgetting to strip punctuation from words, causing 'fun.' and 'fun' to be counted as different words.
  • Not lowering case, so 'Python' and 'python' are treated as separate entries.
  • Using `.count()` on a list inside a loop, which is O(n²) instead of a single dictionary pass.

Variations

  1. Use `collections.Counter(text.split())` for a one-liner word frequency counter.
  2. Return a sorted list of top frequent words with `sorted(word_counts, key=word_counts.get, reverse=True)[:10]`.

Real-world use cases

  • Building a simple spam filter that flags messages based on repeated keyword frequencies.
  • Analyzing customer feedback to identify the most common complaints from support tickets.
  • Preprocessing text data for a natural language model by checking word distribution and vocabulary size.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Dictionaries & sets

Related tutorials and quizzes for this topic.