How to Process Text in Python
This code processes multiline text by splitting lines, stripping whitespace, counting words and characters, and converting to lowercase.
Python code
30 linesdef 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
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
- Use `line.split()` instead of `line.strip().split()` if you don't need cleaned raw text.
- 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
More from Strings & text
- Automatically Detect Weak Passwords from Large Password Lists in Python easy
- Build CSV row from Python list with proper quoting easy
- Build a Secure Password Strength Checker in Python easy
- Convert Natural Language Dates to Datetime in Python medium
- Count Characters, Words, and Lines in Python Text easy
- Extract Data from Strings in Python: Beginner's Guide easy
Keep learning
Related tutorials and quizzes for this topic.