Remove Whitespace in Python

Remove all whitespace characters from a string.

BeginnerString ProgramsExample 7 of 25
remove-whitespace.py
Run in browser
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.