Remove all whitespace in a string
How can you remove all whitespace from a string in Python (or other languages)?
This question explores simple ways to eliminate every space, tab, or newline character from a string, which is useful in data cleaning, formatting, or strict comparisons.
Removing all whitespace from a string is a common task, especially when you're cleaning data or need a precise match. Whitespace includes spaces, tabs ( ), and newlines (
). Thankfully, most programming languages offer simple ways to handle this.
In Python:
Here are a few ways to remove all whitespace from a string:
1. Using str.replace() repeatedly:
text = " Hello World
"
cleaned = text.replace(" ", "").replace(" ", "").replace("
", "")
print(cleaned) # Output: HelloWorld
2. Using regular expressions (more flexible):
import re
text = " Hello World
"
cleaned = re.sub(r"s+", "", text)
print(cleaned) # Output: HelloWorld
s+ matches any whitespace character, including tabs and newlines.
3. Using join() and split():
text = " Hello World
"
cleaned = "".join(text.split())
print(cleaned) # Output: HelloWorld
This method splits the string by any whitespace and rejoins the parts.
When Would You Use This?
- Data preprocessing before analysis or model training.
- Cleaning up user input.
- Comparing strings with inconsistent formatting.
In short, whether you’re coding in Python, JavaScript, or another language, there are efficient ways to strip out all the whitespace. Choose the method that best fits your use case—re.sub() is a great go-to when you want to catch all kinds of spacing.