COGNITION FOUNDRYLEARN. BUILD. UNDERSTAND.
PY-019 / Make incoming text usable◷ 19 min read + practice

Cleaning and normalising text

Real programs receive messy text from people, forms and files. A good cleanup routine removes what is unwanted without silently changing the useful part.

By the end, you can: Clean copied text using strip(), normalise whitespace carefully and explain why blanket cleaning is risky.
Before this lesson: PY-005 · PY-013 · PY-017

Start with the boundaries

The method strip() removes leading and trailing whitespace, including ordinary spaces, tabs and line breaks. It does not remove spaces between words. lstrip() and rstrip() affect only one side.

For a copied Windows path, leading and trailing spaces might be accidental, but interior spaces can belong to the filename. Choose which characters to remove rather than cleaning everything indiscriminately.

When whitespace inside text matters

Calling split() without an argument breaks text at runs of whitespace. Joining those pieces with a single space can normalise user-entered labels or search queries.

Do not apply that trick to file paths, passwords or exact identifiers: replacing interior whitespace can change their meaning. The same utility is helpful in one context and destructive in another.

Compare text without changing the stored value

For case-insensitive comparisons, casefold() is often more appropriate than lower(). It is designed for caseless matching across Unicode text, although it is not a universal language-aware search engine.

Keep the original text when its spelling or capitalisation matters. Normalise a temporary comparison key rather than overwriting data that a user may want to preserve.

Make cleaning a reusable function

A small function such as clean_label(text) can express the one cleaning rule a tool needs. Give it examples of both clean and messy input so you can detect unintended changes.

An important question is whether an empty result is acceptable. That decision belongs to validation, which we will practise again in PY-022.

Try it yourself.

Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.

CF Python PlaygroundBrowser-based Python
Ready to load Python
Trim surrounding whitespace

See what strip() removes while preserving spaces inside the value.

Syntax colours highlight Python code. Tab inserts four spaces; press Escape then Tab to leave the editor. Ctrl+Enter runs the current code.
OUTPUT / ERRORS
Press Run Python to execute the editable code.
NOW MAKE IT YOURS

Add a tab at the beginning and check that it also disappears.

Need a hint?

Use the escaped tab character \t inside the string.

On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.

Check your understanding.

Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.

  1. Why is strip() safer than replacing every space when cleaning a copied Windows file path?

    View answer

    strip() removes surrounding whitespace and preserves the characters inside a path, including spaces that may be part of folder or file names. Replacing every space can turn an existing path into a different path. Even surrounding whitespace should be removed only if it is actually accidental.

  2. When might " ".join(value.split()) be a sensible normalisation step?

    View answer

    " ".join(value.split()) is useful for labels, search terms or similar free-form text when multiple spaces, tabs and newlines should all become single spaces. It is unsafe for values whose exact spacing matters, including paths, passwords and some fixed-format records.

Where this leads.

Next we will search, split and replace specific text instead of normalising whole strings.