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 Character | Description |
U+0020 | Space |
U+00A0 | No-Break Space |
U+1680 | Ogham Space Mark |
U+2000 | En Quad |
U+2001 | Em Quad |
U+2002 | En Space |
U+2003 | Em Space |
U+2004 | Three-Per-Em Space |
U+2005 | Four-Per-Em Space |
U+2006 | Six-Per-Em Space |
U+2007 | Figure Space |
U+2008 | Punctuation Space |
U+2009 | Thin Space |
U+200A | Hair Space |
U+202F | Narrow No-Break Space |
U+205F | Medium Mathematical Space |
U+3000 | Ideographic Space |