Substitute multiple whitespace with single whitespace in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Normalizing whitespace is a small task that shows up everywhere: cleaning form input, preparing log lines, simplifying scraped text, and making test output easier to compare. In Python, the right approach depends on whether you want to collapse all whitespace everywhere or preserve some formatting such as line breaks.
Replacing Runs of Whitespace With a Single Space
If the goal is to convert any sequence of spaces, tabs, or newline characters into one plain space, the most direct tool is re.sub.
Output:
The pattern \s+ means "one or more whitespace characters." That includes ordinary spaces, tabs, and line breaks. Calling .strip() afterward removes any leading or trailing space that may be introduced when the original text starts or ends with whitespace.
This version is a good default when you want a single flat sentence or paragraph.
Using split() and join() for Simpler Cases
Python can do the same cleanup without regular expressions by splitting the string on arbitrary whitespace and joining the pieces back together.
This is concise and readable. It also treats repeated whitespace the same way as the regex example. For many scripts, split() plus join() is enough and easier to explain to teammates who do not work with regex often.
One practical difference is intent. split() plus join() clearly says "tokenize the text into words and rebuild it with single spaces." re.sub() is more flexible if you later need a narrower pattern.
Preserving Line Breaks While Normalizing Each Line
Collapsing all whitespace may be too aggressive if your text has meaningful lines. For example, configuration snippets, user-entered addresses, and plain-text reports often need to keep line boundaries while still shrinking repeated spaces inside each line.
Output:
Here the pattern is [ \t]+, not \s+. That choice collapses only spaces and tabs, which means line breaks survive.
Choosing the Right Technique
Use regex when you need precise control over which whitespace characters count as duplicates. Use split() plus join() when you simply want to collapse all whitespace into single spaces with minimal code.
If performance matters, both approaches are typically fast enough for ordinary application strings. The better question is correctness: are you flattening text intentionally, or are you accidentally destroying layout that should remain visible?
Common Pitfalls
The biggest mistake is using \s+ without realizing that it matches newlines. That is perfect for cleaning a sentence, but it is wrong when your text contains paragraphs, stack traces, or any format where line breaks carry meaning.
Another issue is forgetting to trim the result. Replacing a block of whitespace at the beginning or end of a string with a single space often leaves output that looks almost correct but fails equality checks.
Unicode whitespace can also matter in scraped or copy-pasted text. Python's regex engine generally handles common Unicode whitespace well with \s, but if your data includes unusual separator characters, test with realistic samples.
Finally, do not normalize too early in a pipeline. If you compress whitespace before parsing markdown, CSV fragments, or fixed-width text, you may remove clues that the parser needed.
Summary
- Use
re.sub(r"\s+", " ", text).strip()to collapse all whitespace into one space. - Use
" ".join(text.split())for a compact solution when full flattening is acceptable. - Use
[ \t]+instead of\s+when you need to preserve line breaks. - Add
.strip()when leading or trailing whitespace should disappear. - Decide first whether the text is free-form prose or layout-sensitive content before normalizing whitespace.
Related reading
- Subtract one month from Datetime.Today
- subtuples for a tuple
- Sum a list of numbers in Python
- sum over a list of tensors in tensorflow
- super fails with error TypeError argument 1 must be type, not classobj when parent does not inherit from object
- ''super'' object has no attribute ''__sklearn_tags__''
- super raises TypeError must be type, not classobj for new-style class
- supertype, obj obj must be an instance or subtype of type in Keras
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.