Python String isspace() With Examples

Python String isspace() function returns True if there are only whitespace characters in the string, otherwise it returns False. If the string is empty then isspace() function returns False.

Python String isspace()

Some of the common whitespace characters are t, n, r and obviously whitespace itself.

Let’s look at some examples of isspace() function.


s="   "
print(s.isspace())
s="tnrt "
print(s.isspace())
s="u0009tu200a u3000"
print(s.isspace())

Output:


True
True
True

Printing all whitespace characters Unicode Data

We can use unicodedata module to print all the Unicode character codepoints that are treated as whitespace.


import unicodedata
count = 0
for codepoint in range(2 ** 16):
    ch = chr(codepoint)
    if ch.isspace():
        print(u'{:04x}: ({})'.format(codepoint, unicodedata.name(ch, 'UNNAMED')))
        count = count + 1
print(f'Total Number of Space Unicode Characters = {count}')

Output:


0009: (UNNAMED)
000a: (UNNAMED)
000b: (UNNAMED)
000c: (UNNAMED)
000d: (UNNAMED)
001c: (UNNAMED)
001d: (UNNAMED)
001e: (UNNAMED)
001f: (UNNAMED)
0020: (SPACE)
0085: (UNNAMED)
00a0: (NO-BREAK SPACE)
1680: (OGHAM SPACE MARK)
2000: (EN QUAD)
2001: (EM QUAD)
2002: (EN SPACE)
2003: (EM SPACE)
2004: (THREE-PER-EM SPACE)
2005: (FOUR-PER-EM SPACE)
2006: (SIX-PER-EM SPACE)
2007: (FIGURE SPACE)
2008: (PUNCTUATION SPACE)
2009: (THIN SPACE)
200a: (HAIR SPACE)
2028: (LINE SEPARATOR)
2029: (PARAGRAPH SEPARATOR)
202f: (NARROW NO-BREAK SPACE)
205f: (MEDIUM MATHEMATICAL SPACE)
3000: (IDEOGRAPHIC SPACE)
Total Number of Space Unicode Characters = 29

Did you know there are so many whitespace characters? I certainly did not. 🙂

If you are not familiar with f-prefixed string formatting, please read f-strings in Python.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: