How to Normalize Text in Python
This code defines a function that trims, lowercases, and collapses extra whitespace in a string, returning normalized text.
Python code
8 linesdef normalize_text(text: str) -> str:
normalized = " ".join(text.lower().strip().split())
return normalized
if __name__ == "__main__":
raw = " Hello, WORLD! This is a test. "
print(normalize_text(raw))
Output
hello, world! this is a test.
How it works
The strip() method removes leading and trailing whitespace. Calling split() without arguments splits the string on any runs of whitespace, discarding empty strings and effectively collapsing multiple spaces. join() then reassembles the words with single spaces. lower() makes the text case-insensitive for consistency. The function is safe for any string input and returns a clean, normalized version suitable for comparison, storage, or display.
Common mistakes
- Using `.replace(' ', '')` which removes spaces entirely instead of normalizing them
- Forgetting `strip()` so leading/trailing whitespace remains
- Assuming `.split(' ')` splits on single spaces, leaving empty strings for multiple spaces
Variations
- Use a regex: `re.sub(r'\s+', ' ', text.strip().lower())`
- Create a reusable function that normalizes a list of strings at once
Real-world use cases
- Standardizing user input in search forms to improve matching.
- Cleaning scraped text data before processing in an NLP pipeline.
- Formatting CSV fields to ensure consistent casing and spacing for storage.
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.