Sobes.tech
Junior — Middle

What is the difference between using a tab character and four spaces for code indentation?

sobes.tech AI

Answer from AI

The main difference between using a tab character and four spaces for code indentation in Python is related to editor settings and readability:

  • A tab is a single character that can be displayed with different widths in editors (usually 4 or 8 spaces), which can lead to inconsistent visual alignment among different developers.
  • Four spaces are a fixed number of characters, ensuring consistent display in any editor.

In Python, it is important that indentation is consistent; otherwise, the interpreter will raise an error. It is recommended to use either only tabs or only spaces. PEP 8, the official Python coding style, recommends using 4 spaces for indentation.

Example of correct indentation with spaces:

if True:
    print("Indentation with 4 spaces")

Mixing tabs and spaces can lead to an IndentationError.