Importance Of Whitespaces In Python

In the article “How to use Python interactively”, we learned to use Python REPL. We learned how to use REPL as a calculator. In this article, I will tell you the importance of Whitespace inside Python and how to write code blocks in REPL.

For example, let us take an example of a function that is defined by a code block. When we write any function, loop, or conditional statements in Python, the colon is used at the end of its first line, so that the Python REPL makes sense that the incoming code lines are now in this block.


As you can see in the picture, the colon is used at the end of the first line of the code block (in this case the function). Due to which the three arrow prompt changed to a three dot prompt. Which indicates that the statement written in this prompt is related to its code block.

You can also see that some space is left before writing the print statement in the three-dot prompt. This space matters a lot in Python. We will read about it further in detail. After this, when you have written all the statements in your code block, you will have to leave the last three-dot prompts empty to finish the code block. You can call this function also by simply typing its name and parenthesis as shown in the picture above.

Significant Whitespaces

In this article,  we will know how important Whitespace is in Python. If you have learned the C programming language, then you would know that Whitespaces does not matter in the C language. The code is demarcated with curly brackets. But in Python, Whitespace means a lot.

For example, if you write a function in the C language, you write the function code in a curly bracket. But if you write a function in Python, you need to write its code indent at a higher level than the function’s definition statement with the help of Whitespace.

c code block

You saw in the code above that the code of the main function has been written using the curly brackets in C language. But in Python language, the code is written using white space to write the function code after the def statement. 

One thing to note here is that statements with the same indentation in Python are considered part of a code block. For example, if I forward the print statement “see the indentation level” to one space in the “” program above, then this statement will not be part of the hello function and it gives IndentationError.

python code wrong block

Features of Significant Whitespaces

  • Readability: Significant whitespace provides readability to the code. And as I mentioned earlier in the articles, Python culture places great emphasis on code readability. The code’s readability increases as all statements in a code block are in the same indentation level.
  • No Brackets Clutter: Because the brackets do not have to be used to demarcate the code, the entire code is devoid of the clutter of curly brackets Which gives beauty to the code. And this provides relief from programmers’ trouble of locating and closing brackets.
  • Synchronization: Because while writing the code all developers have to follow the same rule due to which there is coordination between the developers. The program and the programmer are never out of synchronization.

Rules or Conventions

  • Prefer Four Spaces: The Python community has a tradition of using 4 spaces to give indentations to the program. It is not that only 4 spaces are necessary, but the community uses 4 spaces only to give indentation.
  • Never Mix Spaces and Tabs: You can also use Tab instead of spaces. But keep in mind that do not mix the two. If you use Tab then use the same in the entire program and if you use spaces, use the same.
  • Be Consistent: When you write a block, use the same indentation level for all its statements. However, you can use different indentations of different blocks.
  • Allowed to Break Rules: You can deviate from the conventions of Significant Whitespaces, as long as you are increasing the readability of the program.

Here, We will cover Python from basics to advanced level here in Study4Geeks. I assure you that you will enjoy this python journey very much. Stay connected with us to learn. We wish you good health. We really appreciate your valuable time.

Do you know?
Importance Of Whitespaces In Python This article is contribute by Yash Goyal If you want share your knowledge on Study4Geeks then go to login section.