How to Normalize Text in Python

This code defines a function that trims, lowercases, and collapses extra whitespace in a string, returning normalized text.

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

Python code

8 lines
Python 3.9+
def 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

stdout
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

  1. Use a regex: `re.sub(r'\s+', ' ', text.strip().lower())`
  2. 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

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.