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

Example: Check if the string contains only whitespace characters

S = '    '
x = S.isspace()
print(x)    # True
S = '   a'
x = S.isspace()
print(x)	# 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.

Example:

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

Unicode Whitespace Characters

Here are some Unicode characters that qualify as whitespace.

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

Example:

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