Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To collapse runs of whitespace into one ordinary space in Python, use ' '.join(text.split()). It also removes whitespace at the beginning and end of the string. If you need to change repeated space characters without touching tabs or line breaks, use a regular expression that matches literal spaces instead.

How to collapse whitespace in a Python string

Use split() without an argument, then join the resulting words with one space:

text = '  one   twotthree  '
cleaned = ' '.join(text.split())
print(cleaned)
# one two three

With no separator, str.split() treats runs of whitespace as boundaries between pieces. Joining those pieces with ' ' inserts exactly one ordinary space between them. The same operation removes leading and trailing whitespace because the split result does not include empty pieces for those edges. See the Python 3.14.7 Built-in Types reference for the documented behavior of split() and join().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Change repeated spaces but preserve tabs and line breaks

If “spaces” means the ordinary space character only, use a regular expression matching two or more literal spaces:

import re

text = 'one   twotthree'
cleaned = re.sub(r' {2,}', ' ', text)
print(cleaned)
# one twotthree

The pattern {2,} matches a run of at least two ordinary spaces. It does not match the tab in the example, so that character remains unchanged. Unlike the split-and-join idiom, this substitution does not trim the string’s edges unless the pattern itself matches whitespace there.

Choose the method that matches your definition of “space”

Goal Expression Effect
Collapse whitespace runs and trim the edges ' '.join(text.split()) Converts runs of whitespace into one ordinary space and removes leading and trailing whitespace.
Collapse only repeated ordinary spaces re.sub(r' {2,}', ' ', text) Changes repeated spaces while leaving tabs and line breaks alone.
Collapse runs of regex-recognized whitespace re.sub(r's+', ' ', text) Replaces each run recognized by s with one ordinary space. In Unicode string patterns, Python’s regex documentation defines s according to str.isspace().

For the regular-expression behavior of substitutions and s, see Python’s Regular Expression HOWTO.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why split(' ') and replace() may not work

Use split(), not split(' '), to group whitespace

An explicit separator changes how splitting works. text.split(' ') looks for literal spaces, and consecutive delimiters can produce empty strings rather than treating the run as one separator. For general whitespace normalization, omit the separator: text.split().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A single replace() call is not a run matcher

str.replace(old, new) replaces occurrences of the exact substring you specify; it does not interpret a pattern. For example, text.replace(' ', ' ') targets pairs of spaces, not arbitrary whitespace. A longer run can still contain repeated pairs after one pass, so this is not a dependable general solution. Use split-and-join for broad whitespace normalization or a repeated-character regex when the desired scope is precise. Python documents replace() in its Built-in Types reference.

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.