Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →To collapse runs of whitespace into one ordinary space in Python, use ' '.join(text.split()). It also removes whitespace from the beginning and end of the string. If you want to collapse only repeated literal spaces while preserving tabs and line breaks, use re.sub(r' {2,}', ' ', text).
Collapse whitespace and trim the string
Call split() without an argument to treat each run of whitespace as a separator, then join the resulting pieces with one ordinary space:
As an Amazon Associate I earn from qualifying purchases.
text = ' one twotthree '
cleaned = ' '.join(text.split())
print(cleaned)
# one two three
This built-in approach collapses whitespace runs and removes leading and trailing whitespace. The tab between two and three is treated as whitespace too. See Python’s string methods documentation for split() and join().
Change repeated spaces without changing tabs or newlines
If “spaces” means only the ordinary space character, use a regular expression that matches two or more spaces:
#1 Best Overall
import re
text = 'one twotthree'
cleaned = re.sub(r' {2,}', ' ', text)
print(cleaned)
# one twotthree
The pattern {2,} matches repeated literal spaces, not tabs or line breaks, so those characters remain unchanged. re.sub() replaces matches with the supplied replacement string; Python documents this behavior in its Regular Expression HOWTO.
Choose the right method for the input
| Goal | Expression | Effect |
|---|---|---|
| Collapse whitespace runs and trim the edges | ' '.join(text.split()) |
Replaces each whitespace-separated boundary with one ordinary space and drops leading or trailing whitespace. |
| Collapse repeated literal spaces only | re.sub(r' {2,}', ' ', text) |
Changes runs of ordinary spaces; leaves tabs and line breaks alone. |
| Collapse regex-recognized whitespace | re.sub(r's+', ' ', text) |
Replaces runs matched by s; for Unicode string patterns, Python’s regex documentation defines this in relation to str.isspace(). |
| Replace a known exact substring | text.replace(old, new) |
Replaces occurrences of that substring; it does not match a pattern for arbitrary runs. |
Avoid common string-method pitfalls
Do not pass a space to split when you want grouped separators
text.split(' ') uses a literal space as its separator. Consecutive separators can produce empty strings rather than being grouped as one whitespace run. For general whitespace normalization, leave the argument out: text.split().
Rank #2
Do not expect split and join to preserve edge whitespace
' '.join(text.split()) intentionally removes whitespace at the start and end. If only internal literal spaces should change and edge characters must remain untouched, use the literal-space regular expression instead.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →A single replace call is not a general run-collapsing operation
text.replace(' ', ' ') replaces occurrences of the exact two-space substring. It is not a pattern matcher for arbitrary whitespace, so it is not the clearest general solution for runs of different lengths. Python describes str.replace() as returning a copy with occurrences of the specified substring replaced in its Built-in Types reference.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




