How to Process Text in Python

This code processes multiline text by splitting lines, stripping whitespace, counting words and characters, and converting to lowercase.

Easy Python 3.9+ Aug 9, 2026 Strings & text 13 views 0 copies

Python code

30 lines
Python 3.9+
def process_text(text):
    lines = text.split("\n")
    clean_lines = []
    for line in lines:
        stripped = line.strip()
        if stripped:
            tokens = stripped.split()
            title_case = stripped.lower()
            clean_lines.append({
                "raw": stripped,
                "word_count": len(tokens),
                "char_count": len(stripped),
                "words": tokens,
                "lowercase": title_case
            })
    total_words = sum(item["word_count"] for item in clean_lines)
    total_chars = sum(item["char_count"] for item in clean_lines)
    return {
        "lines": clean_lines,
        "total_lines": len(clean_lines),
        "total_words": total_words,
        "total_chars": total_chars
    }

if __name__ == "__main__":
    sample = "\n  Python is fun!  \n  Learn strings today.  \n  Practice text processing.  \n"
    result = process_text(sample)
    print(result["total_lines"], result["total_words"], result["total_chars"])
    for line in result["lines"]:
        print(line["lowercase"])

Output

stdout
3 9 46
python is fun!
learn strings today.
practice text processing.

How it works

The function process_text first splits the input text into lines using split("\n"). Each line is stripped of leading/trailing whitespace and empty lines are skipped. For each non-empty line, it counts words and characters, splits into tokens, and converts to lowercase. The function accumulates totals for lines, words, and characters across all processed lines. This structured approach makes it easy to analyze text data line-by-line and is a common first step in many text-processing workflows.

Common mistakes

  • Forgetting to strip whitespace before counting characters or words, leading to inflated counts.
  • Assuming all lines have content and not filtering empty lines, which skews totals.
  • Mixing up the order of operations (e.g., counting characters before stripping).
  • Not converting to lowercase consistently if case-insensitive analysis is required.

Variations

  1. Use `line.split()` instead of `line.strip().split()` if you don't need cleaned raw text.
  2. Return a list of dictionaries directly without aggregating totals for simpler use cases.

Real-world use cases

  • Cleaning and summarizing user-generated comments or reviews before analysis.
  • Preprocessing log files to extract and count event messages per line.
  • Building a simple text statistics tool for documentation or content management systems.

Sponsored

Run this sample

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

Open editor

More from Strings & text

Related tutorials and quizzes for this topic.