Python String isspace() Method

Determines whether the string contains only whitespace characters

Usage

The isspace() method returns TRUE if the string is nonempty and all characters in it are whitespace characters. Otherwise, it returns FALSE.

Syntax

string.isspace()

Basic Example

# Check if the string contains only whitespace characters
S = '    '
x = S.isspace()
print(x)
# Prints True
S = '   a'
x = S.isspace()
print(x)
# Prints False

ASCII Whitespace Characters

The most common whitespace characters are space ' ' , tab '\t' , and newline '\n'. Carriage Return '\r' and ASCII Form Feed '\f' are also considered as whitespace characters.

S = ' \t \n \r \f '
x = S.isspace()
print(x)
# Prints True

Unicode Whitespace Characters

Some Unicode characters qualify as whitespace.

S = '\u2005 \u2007'
x = S.isspace()
print(x)
# Prints True

Here is a complete list:

Unicode Whitespace characters
Unicode CharacterDescription
U+0020Space
U+00A0No-Break Space
U+1680Ogham Space Mark
U+2000En Quad
U+2001Em Quad
U+2002En Space
U+2003Em Space
U+2004Three-Per-Em Space
U+2005Four-Per-Em Space
U+2006Six-Per-Em Space
U+2007Figure Space
U+2008Punctuation Space
U+2009Thin Space
U+200AHair Space
U+202FNarrow No-Break Space
U+205FMedium Mathematical Space
U+3000Ideographic Space