Python 3 reference · String Methods

Python String isspace() Method

Return True if every character is whitespace (spaces, tabs, newlines) and the string isn't empty.

Python 3 Since 3.0

Syntax

str.isspace()

Return value

True or False.

Examples

Whitespace only?Try it yourself
print("   ".isspace(), " \t\n".isspace(), "".isspace(), " a ".isspace())

Output

True True False False
Skip blank lines (a common pattern)Try it yourself
lines = ["first", "   ", "", "second"]
print([l for l in lines if l.strip()])

Output

['first', 'second']

Version history

VersionChange
Python 3.0Added