Skip to content

Text Processor

Easy ~20 min

What you'll build

A Python script with functions to clean whitespace, count word frequency, find and replace, and convert case — a mini text toolkit.

Goals

  • Use string methods (strip, split, replace, lower) to clean text
  • Count word frequency with a dictionary
  • Build reusable functions with clear parameters
  • Format a readable summary report

Prerequisites

  • Strings and string methods
  • Dictionaries
  • Functions

Steps

  1. 1

    Write the text cleaning function

    Define clean_text(text) that strips leading/trailing whitespace, collapses multiple spaces into one, and converts to lowercase. This normalizes text for consistent processing.

    Code
    def clean_text(text):
        import re
        text = text.strip().lower()
        text = re.sub(r'\s+', ' ', text)
        return text

    Expected result

    clean_text(' Hello World ') returns 'hello world'.

  2. 2

    Count word frequency

    Define word_frequency(text) that splits text into words and counts each word using a dictionary. Return the dict sorted by frequency (highest first).

    Code
    def word_frequency(text):
        words = text.split()
        freq = {}
        for word in words:
            freq[word] = freq.get(word, 0) + 1
        return dict(sorted(freq.items(), key=lambda x: x[1], reverse=True))

    Expected result

    word_frequency('a b a c a') returns {'a': 3, 'b': 1, 'c': 1}.

  3. 3

    Build the report function

    Define report(text) that cleans the text, counts words and characters, finds the top 3 words, and prints a formatted summary.

    Code
    def report(text):
        cleaned = clean_text(text)
        freq = word_frequency(cleaned)
        print(f"Characters: {len(cleaned)}")
        print(f"Unique words: {len(freq)}")
        print("Top 3 words:")
        for word, count in list(freq.items())[:3]:
            print(f"  {word}: {count}")

    Expected result

    Calling report on a sample text prints character count, unique word count, and top 3 words.

  4. 4

    Add input validation & exception handling

    Add a try/except block in the main function to handle user input errors, using ValueError to catch illegal conversions. Python's exception handling lets the program degrade gracefully on bad input instead of crashing.

    Code
    def safe_input(prompt, validator=None):
        """Read input with validation and error handling."""
        while True:
            try:
                raw = input(prompt)
                value = float(raw)
                if validator and not validator(value):
                    raise ValueError(f"Value {value} failed validation")
                return value
            except ValueError as err:
                print(f"Invalid input: {err}. Please try again.")
    
    # Usage
    age = safe_input("Enter your age: ", lambda x: x > 0)

    Expected result

    Invalid input (non-number or failed validation) prompts for re-entry; valid input returns the number.

  5. 5

    Write unit tests

    Write tests for the core function using unittest or assert statements, covering normal paths, boundary values, and exceptions. Tests are the safety net for code quality.

    Code
    import unittest
    
    class TestCoreFunc(unittest.TestCase):
        def test_normal(self):
            self.assertEqual(core_func(5), expected_5)
    
        def test_boundary(self):
            self.assertEqual(core_func(0), expected_0)
    
        def test_exception(self):
            with self.assertRaises(ValueError):
                core_func(-1)
    
    if __name__ == "__main__":
        unittest.main()

    Expected result

    Running python test.py prints OK with all three test cases passing.

Try it yourself

What you learned

You built a text processing toolkit combining string methods, dictionary counting, and sorting — the same patterns used in log analysis, search engines, and NLP pipelines.

Related Resources

Related Lessons

Related Practice

Related Cheatsheets

Related Errors

Related Glossary

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

Related Errors

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

← Back to topic