Python
whitespace
string manipulation
coding
duplicate

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.

Browse interview questions

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.

python
1import re
2
3text = "Alice\t\tworks\n\nin   Toronto."
4cleaned = re.sub(r"\s+", " ", text).strip()
5
6print(cleaned)

Output:

text
Alice works in Toronto.

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.

python
1text = "Alice\t\tworks\n\nin   Toronto."
2cleaned = " ".join(text.split())
3
4print(cleaned)

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.

python
1import re
2
3text = "Name:   Alice\nCity:\t\tToronto\n\nRole:   Engineer"
4
5normalized_lines = [
6    re.sub(r"[ \t]+", " ", line).strip()
7    for line in text.splitlines()
8]
9
10cleaned = "\n".join(normalized_lines)
11print(cleaned)

Output:

text
1Name: Alice
2City: Toronto
3
4Role: Engineer

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.