In this article we will take a quick look at how Python parses white space in source code. We will also touch on some of the benefits of using Python over other languages when it comes to handling white space. As anyone who has written Python code knows, indentation is very important in the language. In fact, it is so important that the official Python style guide, PEP 8, includes a section on it. This is due to the fact that whitespace is used to denote structure in Python.
What is white space?
Python uses whitespace to structure code. Specifically, it relies on indentation to create blocks of code.
This can be seen in the way that if statements and for loops are written in Python. Consider the following example:
“`python
if True:
print(“Hello, world!”)
“`
In this example, the `if` statement is followed by a block of code (the `print` statement). The block of code is indented by four spaces. This is how Python knows that the `print` statement belongs to the `if` statement. Without the indentation, Python would not know how to parse the code correctly.
Whitespace is also used within blocks of code to make them easier to read. For example, consider the following list comprehension:
“`python
my_list = [i**2 for i in range(10)]
“`
This is valid Python code, but it is not very readable. It would be much better if it were written like this:
“`python
my_list = [i**2
for i in range(10)]
“`
This is called “pretty-printed” code, and it is generally considered to be better style.
How does Python handle white space?
Python treats whitespace as significant and uses it to structure code. For example, Python uses indentation to delimit code blocks, so whitespace at the beginning of a line can be used to control the flow of execution.
In addition, Python relies on white space to delimit string literals. For example, consider the following code:
s = ‘This is a string’
Here, the string literal is delimited by the quotation marks. If we wanted to include a quotation mark within the string, we would need to escape it like so:
s = ‘This is a \”string\”‘
Finally, Python also uses whitespace to format code for readability. For example, it is common to use whitespace to indent code blocks.
What are the benefits of this approach?
Python’s use of whitespace as a delimiter can offer some benefits over other languages that use more traditional delimiters such as commas or semicolons. One benefit is that it can make code more readable, since Python programmers generally use four spaces for each indentation level, making the structure of the code clearer. Additionally, using whitespace can avoid the need for escape characters when dealing with strings, since quotes and other characters can simply be left as-is without worrying about whether they will be interpreted as part of the code or not. Finally, this approach can also make it easier to modify existing code, since adding or removing indentation levels is generally a much simpler task than having to add or remove punctuation characters.
Are there any drawbacks?
Python’s use of whitespace can be a drawback for some programmers. For example, if you’re used to programming in a language like C++ or Java, which uses curly braces to denote code blocks, the lack of braces in Python can be confusing. Also, because Python relies on indentation to indicate code blocks, it’s important to be consistent with your indentation; otherwise, your code may not run correctly.


