For code indentation, is a single tab equivalent to 2 spaces or 4 spaces?


#1

Question

In Python, is a single tab equivalent to 2 spaces or 4 spaces, and if so can they be mixed?

Answer

In most code editors, tabs are not the same as 2 spaces or 4 spaces by default.

A tab is stored differently than spaces in the code. Tabs can be seen as a big “jump” in the text, while spaces are always 1 space each. When you move your cursor in the code, you may notice this “jump” when going through tabs as opposed to spaces.

Unlike computers, we can’t easily tell whether an indentation is a tab or spaces. Not knowing this can cause potential errors in the code, and mixing them or wrongly applying them will cause an IndentationError. Because of this, staying consistent with using spaces or tabs in a program can be very important and save a lot of trouble.