Remove Whitespace in Python

Remove all whitespace characters from a string.

BeginnerString ProgramsExample 7 of 25
remove-whitespace.py
1# Program to remove all whitespace from a string
2
3s = input("Enter a string: ")
4
5no_space = "".join(ch for ch in s if not ch.isspace())
6
7print("Without whitespace:", no_space)

Output

Enter a string: hello world
Without whitespace: helloworld

What's going on

We filter out characters for which .isspace() is True.

After this program

If the idea is still fuzzy, read Day 4: Strings and String Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 string programs, or see every topic on Python exercises.