Whitespace is used to denote blocks. In other languages curly brackets ({ and }) are common. When you indent, it becomes a child of the previous line. In addition to the indentation, the parent also has a colon following it.

im_a_parent:
    im_a_child:
        im_a_grandchild
    im_another_child:
        im_another_grand_child

Off the top of my head, def, if, elif, else, try, except, finally, with, for, while, and class all start blocks. To end a block, you simple outdent, and you will have siblings. In the above im_a_child and im_another_child are siblings.

Answer from Brigand on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ string-whitespace-in-python
string.whitespace in Python - GeeksforGeeks
July 11, 2025 - In Python, string.whitespace is a string containing all the characters that are considered whitespace. Whitespace characters include spaces, tabs, newlines and other characters that create space in text.
Top answer
1 of 5
12

Whitespace is used to denote blocks. In other languages curly brackets ({ and }) are common. When you indent, it becomes a child of the previous line. In addition to the indentation, the parent also has a colon following it.

im_a_parent:
    im_a_child:
        im_a_grandchild
    im_another_child:
        im_another_grand_child

Off the top of my head, def, if, elif, else, try, except, finally, with, for, while, and class all start blocks. To end a block, you simple outdent, and you will have siblings. In the above im_a_child and im_another_child are siblings.

2 of 5
9

Whitespace just means characters which are used for spacing, and have an "empty" representation. In the context of python, it means tabs and spaces (it probably also includes exotic unicode spaces, but don't use them). The definitive reference is here: http://docs.python.org/2/reference/lexical_analysis.html#indentation

I'm not sure exactly how to use it.

Put it at the front of the line you want to indent. If you mix spaces and tabs, you'll likely see funky results, so stick with one or the other. (The python community usually follows PEP8 style, which prescribes indentation of four spaces).

You need to create a new indent level after each colon:

for x in range(0, 50):
    print x
    print 2*x

print x

In this code, the first two print statements are "inside" the body of the for statement because they are indented more than the line containing the for. The third print is outside because it is indented less than the previous (nonblank) line.

If you don't indent/unindent consistently, you will get indentation errors. In addition, all compound statements (i.e. those with a colon) can have the body supplied on the same line, so no indentation is required, but the body must be composed of a single statement.

Finally, certain statements, like lambda feature a colon, but cannot have a multiline block as the body.

๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Python and whitespace - Python Help - Discussions on Python.org
August 1, 2023 - I tried umpteen possibilities why my Python code would not run. In the end, by juggling text editors, I discovered that the whitespace before โ€œreturnโ€ was a tab, not spaces. All the other whitespace was space characters. How could I have detected this earlier?
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ standard-library โ€บ str โ€บ isspace
Python str isspace() - Check for Whitespace | Vultr Docs
December 27, 2024 - The isspace() method in Python is a string method used to check if all characters in a string are whitespace.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ glossary โ€บ whitespace
whitespace | Python Glossary โ€“ Real Python
The most common whitespace characters are spaces, tabs (\t), and newlines (\n). Python uses whitespace for indentation, which defines the structure of code blocks instead of using curly braces or keywords.
๐ŸŒ
LabEx
labex.io โ€บ questions โ€บ how-to-handle-whitespace-characters-in-python-strings-53
How to Handle Whitespace Characters in Python | LabEx
July 25, 2024 - Learn how to identify manipulate and remove whitespace characters in Python strings with practical examples and built-in methods
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_isspace.asp
Python String isspace() Method
Check if all the characters in the text are whitespaces: txt = " s " x = txt.isspace() print(x) Try it Yourself ยป ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
Quora
quora.com โ€บ What-is-whitespace-in-Python-programming
What is whitespace in Python programming? - Quora
Python (programming langu... ... In computer programming, whitespace is any character or series of characters that represent horizontal or vertical space in typography.
Find elsewhere
Top answer
1 of 1
27

Is there a Python constant for Unicode whitespace?

Short answer: No. I have personally grepped for these characters (specifically, the numeric code points) in the Python code base, and such a constant is not there.

The sections below explains why it is not necessary, and how it is implemented without this information being available as a constant. But having such a constant would also be a really bad idea.

If the Unicode Consortium added another character/code-point that is semantically whitespace, the maintainers of Python would have a poor choice between continuing to support semantically incorrect code or changing the constant and possibly breaking pre-existing code that might (inadvisably) make assumptions about the constant not changing.

How could it add these character code-points? There are 1,111,998 possible characters in Unicode. But only 120,672 are occupied as of version 8. Each new version of Unicode may add additional characters. One of these new characters might be a form of whitespace.

The information is stored in a dynamically generated C function

The code that determines what is whitespace in unicode is the following dynamically generated code.

# Generate code for _PyUnicode_IsWhitespace()
print("/* Returns 1 for Unicode characters having the bidirectional", file=fp)
print(" * type 'WS', 'B' or 'S' or the category 'Zs', 0 otherwise.", file=fp)
print(" */", file=fp)
print('int _PyUnicode_IsWhitespace(const Py_UCS4 ch)', file=fp)
print('{', file=fp)
print('    switch (ch) {', file=fp)
for codepoint in sorted(spaces):
    print('    case 0x%04X:' % (codepoint,), file=fp)
print('        return 1;', file=fp)
print('    }', file=fp)
print('    return 0;', file=fp)
print('}', file=fp)
print(file=fp)

This is a switch statement, which is a constant code block, but this information is not available as a module "constant" like the string module has. It is instead buried in the function compiled from C and not directly accessible from Python.

This is likely because as more code points are added to Unicode, we would not be able to change constants for backwards compatibility reasons.

The Generated Code

Here's the generated code currently at the tip:

int _PyUnicode_IsWhitespace(const Py_UCS4 ch)
{
    switch (ch) {
    case 0x0009:
    case 0x000A:
    case 0x000B:
    case 0x000C:
    case 0x000D:
    case 0x001C:
    case 0x001D:
    case 0x001E:
    case 0x001F:
    case 0x0020:
    case 0x0085:
    case 0x00A0:
    case 0x1680:
    case 0x2000:
    case 0x2001:
    case 0x2002:
    case 0x2003:
    case 0x2004:
    case 0x2005:
    case 0x2006:
    case 0x2007:
    case 0x2008:
    case 0x2009:
    case 0x200A:
    case 0x2028:
    case 0x2029:
    case 0x202F:
    case 0x205F:
    case 0x3000:
        return 1;
    }
    return 0;
}

Making your own constant:

The following code (from my answer here), in Python 3, generates a constant of all whitespace:

import re
import sys

s = ''.join(chr(c) for c in range(sys.maxunicode+1))
ws = ''.join(re.findall(r'\s', s))

As an optimization, you could store this in a code base, instead of auto-generating it every new process, but I would caution against assuming that it would never change.

>>> ws
'\t\n\x0b\x0c\r\x1c\x1d\x1e\x1f \x85\xa0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000'

(Other answers to the question linked show how to get that for Python 2.)

Remember that at one point, some people probably thought 256 character encodings was all that we'd ever need.

>>> import string
>>> string.whitespace
' \t\n\r\x0b\x0c'

If you're insisting on keeping a constant in your code base, just generate the constant for your version of Python, and store it as a literal:

unicode_whitespace = u'\t\n\x0b\x0c\r\x1c\x1d\x1e\x1f \x85\xa0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000'

The u prefix makes it unicode in Python 2 (2.7 happens to recognize the entire string above as whitespace too), and in Python 3 it is ignored as string literals are unicode by default.

๐ŸŒ
Janis Lesinskis
lesinskis.com โ€บ python-unicode-whitespace.html
Janis Lesinskis' Blog - Unicode whitespaces in Python
April 20, 2020 - To get more coverage I made this list of unicode space characters: UNICODE_WHITESPACE_CHARACTERS = [ "\u0009", # character tabulation "\u000a", # line feed "\u000b", # line tabulation "\u000c", # form feed "\u000d", # carriage return "\u0020", # space "\u0085", # next line "\u00a0", # no-break space "\u1680", # ogham space mark "\u2000", # en quad "\u2001", # em quad "\u2002", # en space "\u2003", # em space "\u2004", # three-per-em space "\u2005", # four-per-em space "\u2006", # six-per-em space "\u2007", # figure space "\u2008", # punctuation space "\u2009", # thin space "\u200A", # hair space "\u2028", # line separator "\u2029", # paragraph separator "\u202f", # narrow no-break space "\u205f", # medium mathematical space "\u3000", # ideographic space ]
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ isspace
Python String isspace()
s = '\t \n' if s.isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters') s = '2+2 = 4' if s.isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters.')
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-string-isspace-method
Python String isspace() Method - Learn By Example
April 20, 2020 - 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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-string-isspace-method
Python String isspace() Method - GeeksforGeeks
January 2, 2025 - isspace() method in Python is used to check if all characters in a string are whitespace characters. This includes spaces (' '), tabs (\t), newlines (\n), and other Unicode-defined whitespace characters.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-whitespace-constant-in-python
What is the whitespace constant in Python?
The string module in Python is a collection of different constants. The whitespace constant in the string module contains the characters that are considered whitespace.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ python-string-isspace-method
Python String | isspace() method with Examples - Javatpoint
Python isspace() method is used to check space in the string. It returna true if there are only whitespace characters in the string. Otherwise it returns false. Space, newline, and tabs etc are known as whitespace characters and are defined in the Unicode character database as Other or Separator ...
๐ŸŒ
Python
docs.python.org โ€บ 2.0 โ€บ lib โ€บ module-string.html
4.1 string -- Common string operations
A string containing all characters that are considered whitespace. On most systems this includes the characters space, tab, linefeed, return, formfeed, and vertical tab.
๐ŸŒ
Toppr
toppr.com โ€บ guides โ€บ python-guide โ€บ references โ€บ methods-and-functions โ€บ methods โ€บ string โ€บ isspace โ€บ python-string-isspace
Python isspace() function | Why do we use Python String isspace()? |
August 26, 2021 - The Python isspace() String function examines a string for whitespace characters and returns True only if the string contains all spacing characters; else it returns False. Python isspace() is a built-in method that returns True if all of the characters in the string are spacing characters ...
๐ŸŒ
Interactive Chaos
interactivechaos.com โ€บ en โ€บ python โ€บ function โ€บ stringwhitespace
string.whitespace | Interactive Chaos
May 2, 2021 - Python scenarios ยท Full name ยท ... That is, it returns all characters considered whitespace, which includes the characters space, tab, linefeed, return, formfeed, and vertical tab....
๐ŸŒ
Medium
medium.com โ€บ swlh โ€บ whitespace-ive-got-a-blank-space-449f8140011
Whitespace-Iโ€™ve Got a Blank Space | by Sara Khandaker | The Startup | Medium
November 23, 2020 - It's a fairly easy language to ... โ€œIn computer programming, whitespace is any character or series of characters that represent horizontal or vertical space in typography.โ€...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python #7 Whitespaces - YouTube
In programming, whitespace refers to any nonprinting character, such asspaces, tabs, and end-of-line symbols. You can use whitespace to organizeyour output s...
Published ย  August 5, 2022