Python: Remove all whitespaces from a string
40. Remove All Whitespace
Write a Python program to remove all whitespaces from a string.
Sample Solution:-
Python Code:
import re
text1 = ' Python Exercises '
print("Original string:",text1)
print("Without extra spaces:",re.sub(r'\s+', '',text1))
Sample Output:
Original string: Python Exercises Without extra spaces: PythonExercises
Pictorial Presentation:
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to remove all whitespace characters from a string.
- Write a Python script to strip all spaces, tabs, and newline characters from a given text.
- Write a Python program to delete every whitespace in a string and then output the condensed result.
- Write a Python program to process a text and return the version with no whitespace at all.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to remove multiple spaces in a string.
Next: Write a Python program to remove everything except alphanumeric characters from a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.