Spaces are not treated as equivalent to tab. A line indented with a tab is at a different indentation from a line indented with 1, 2, 4 or 8 spaces.

Proof by counter-example (erroneous, or, at best, limited - tab != 4 spaces):

x = 1
if x == 1:
^Iprint "fff\n"
    print "yyy\n"

The '^I' shows a TAB. When run through Python 2.5, I get the error:

  File "xx.py", line 4
    print "yyy\n"
                ^
IndentationError: unindent does not match any outer indentation level

Thus showing that in Python 2.5, tabs are not equal to spaces (and in particular not equal to 4 spaces).


Oops - embarrassing; my proof by counter-example shows that tabs are not equivalent to 4 spaces. As Alex Martelli points out in a comment, in Python 2, tabs are equivalent to 8 spaces, and adapting the example with a tab and 8 spaces shows that this is indeed the case.

x = 1
if x != 1:
^Iprint "x is not 1\n"
        print "y is unset\n"

In Python 2, this code works, printing nothing.


In Python 3, the rules are slightly different (as noted by Antti Haapala). Compare:

  • Python 2 on Indentation
  • Python 3 on Indentation

Python 2 says:

First, tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line’s indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.

Python 3 says:

Tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line’s indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.

(Apart from the opening word "First," these are identical.)

Python 3 adds an extra paragraph:

Indentation is rejected as inconsistent if a source file mixes tabs and spaces in a way that makes the meaning dependent on the worth of a tab in spaces; a TabError is raised in that case.

This means that the TAB vs 8-space example that worked in Python 2 would generate a TabError in Python 3. It is best — necessary in Python 3 — to ensure that the sequence of characters making up the indentation on each line in a block is identical. PEP8 says 'use 4 spaces per indentation level'. (Google's coding standards say 'use 2 spaces'.)

🌐
Reddit
reddit.com › r/learnpython › tabs vs spaces (i don't get it)
r/learnpython on Reddit: Tabs vs Spaces (I don't get it)
April 14, 2018 -

Hey Guys,

I've been following a Guide to python where they told us to use Tabs but then they sent us to the PEP8 Style Guide where it states:

"Spaces are the preferred indentation method."

Now, after I digging around a bit I found that you should use 4 spaces to indent code, sooo...

Why not just use Tab instead of hitting the space bar 4 times?

I'm guessing this is a hot topic, but I just wanted to make sure it's ok with going with Tabs over spaces (since it's the same lenght!). (I'm also a fan of Silicon Valley and as Richard Hendriks says: "2 Tabs save you 8 spaces"!)

is pressing tab not the same thing as four spaces? Jan 22, 2012
r/learnprogramming
14y ago
Tabs or Spaces? Jul 15, 2025
r/learnpython
last yr.
4 space tab or 2? Feb 23, 2023
r/Frontend
3y ago
More results from reddit.com
Top answer
1 of 7
48

Spaces are not treated as equivalent to tab. A line indented with a tab is at a different indentation from a line indented with 1, 2, 4 or 8 spaces.

Proof by counter-example (erroneous, or, at best, limited - tab != 4 spaces):

x = 1
if x == 1:
^Iprint "fff\n"
    print "yyy\n"

The '^I' shows a TAB. When run through Python 2.5, I get the error:

  File "xx.py", line 4
    print "yyy\n"
                ^
IndentationError: unindent does not match any outer indentation level

Thus showing that in Python 2.5, tabs are not equal to spaces (and in particular not equal to 4 spaces).


Oops - embarrassing; my proof by counter-example shows that tabs are not equivalent to 4 spaces. As Alex Martelli points out in a comment, in Python 2, tabs are equivalent to 8 spaces, and adapting the example with a tab and 8 spaces shows that this is indeed the case.

x = 1
if x != 1:
^Iprint "x is not 1\n"
        print "y is unset\n"

In Python 2, this code works, printing nothing.


In Python 3, the rules are slightly different (as noted by Antti Haapala). Compare:

  • Python 2 on Indentation
  • Python 3 on Indentation

Python 2 says:

First, tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line’s indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.

Python 3 says:

Tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line’s indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.

(Apart from the opening word "First," these are identical.)

Python 3 adds an extra paragraph:

Indentation is rejected as inconsistent if a source file mixes tabs and spaces in a way that makes the meaning dependent on the worth of a tab in spaces; a TabError is raised in that case.

This means that the TAB vs 8-space example that worked in Python 2 would generate a TabError in Python 3. It is best — necessary in Python 3 — to ensure that the sequence of characters making up the indentation on each line in a block is identical. PEP8 says 'use 4 spaces per indentation level'. (Google's coding standards say 'use 2 spaces'.)

2 of 7
24

Follow PEP 8 for Python style. PEP 8 says: Indentation

Use 4 spaces per indentation level.

For really old code that you don't want to mess up, you can continue to use 8-space tabs.

Tabs or Spaces?

Never mix tabs and spaces.

The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

Discussions

For code indentation, is a single tab equivalent to 2 spaces or 4 spaces?
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, ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
23
July 19, 2018
Python: using 4 spaces for indentation. Why? - Stack Overflow
I am not affiliated with Sublime ... just for python, and default 'tab_size' = 2 for other languages (like JavaScript etc). 2012-09-17T04:38:37.167Z+00:00 ... I cannot be more with you @Markus :) Accepting the status quo when the difference is in the end quite minimal is what draws the line with being pedantic. 2017-01-25T11:34:43.56Z+00:00 ... "A common question beginners ask is, "How many spaces should I ... More on stackoverflow.com
🌐 stackoverflow.com
The Python tab setting should default to 4 spaces to respect PEP8 and the Google style guide
Currently, when opening a Python notebook, the default tab size is 2. It can be changed with a setting (tools -> preference -> editor -> use 4 spaces), but the default should represent the most widely used workflow. PEP8 recommends using 4 spaces: source The Google Python style guide recommend using 4 spaces: source The Python formatter Black indents ... More on github.com
🌐 github.com
4
October 25, 2019
Why does Python see a tab as 8 spaces? - Stack Overflow
Using a tab width of 4 spaces has emerged as the primary choice in programming. So why doesn't Python see a tab as 4 spaces instead of 8? Is there a (technical, historic, other) reason for this de... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Script Everything
scripteverything.com › home › python › how many spaces is a tab? 2, 4 or 8 explained
How Many Spaces Is a Tab? 2, 4 or 8 Explained
May 17, 2021 - A tab is one character, not a fixed number of spaces. Code editors commonly display tabs as 2 or 4 spaces. Traditional terminals commonly use 8-column stops. Python style uses 4 spaces for indentation.
🌐
The Science 360
thescience360.com › home › gadgets › how many spaces make a tab? with details
How Many Spaces Make a Tab? with Details : The Science 360
December 20, 2023 - Understanding and adhering to indentation conventions is crucial for maintaining clean and readable code. Here are examples of indentation preferences in various programming languages: Python: 4 spaces per indentation ...
🌐
Trey Hunner
treyhunner.com › tabs-and-spaces
Mixing Tabs & Spaces
These two code blocks are also equivalent. Note that on the right, the `if` statement line is indented with 4 spaces and 1 tab, but Python sees this as equivalent to 8 spaces. There is a tab stop every 8 characters in Python.
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
For code indentation, is a single tab equivalent to 2 spaces or 4 spaces? - Python FAQ - Codecademy Forums
July 19, 2018 - 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.
Find elsewhere
🌐
Clarice-note
clarice-note.com › home › how many spaces is a tab? everything you need to know
How Many Spaces Is a Tab? Everything You Need to Know
May 11, 2026 - ... Spaces are individual blank ... even depend on indentation for functionality. Python developers usually follow a standard of four spaces per indentation level....
🌐
Python Forum
python-forum.io › thread-20713.html
Tab character in a string prints 8 spaces
Hi, I'm starting an online Python class and have been using Python 3 latest with IDLE. I open Python first then open IDLE next to it. Well, I got to the section using the newline and tab characters when printing strings. Then noticed something pecu...
🌐
Quora
quora.com › How-many-spaces-or-tab-size-do-you-prefer-to-indent-with-on-Python
How many spaces or tab size do you prefer to indent with on Python? - Quora
Answer (1 of 7): In Python 2 indenting works this way: > First, tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total num...
🌐
Codecademy
codecademy.com › forum_questions › 50115dd0b230bf0002022255
2.2 tab or 4 spaces | Codecademy
The PEP 8, which is the most spread standard, recommends the use of four spaces. You cannot mix tabs with spaces as this will mess up the syntax-parsing, but instead of throwing undefined behavoir, python is usually clever enough to warn you.
🌐
GitHub
github.com › googlecolab › colabtools › issues › 827
The Python tab setting should default to 4 spaces to respect PEP8 and the Google style guide · Issue #827 · googlecolab/colabtools
October 25, 2019 - Currently, when opening a Python notebook, the default tab size is 2. It can be changed with a setting (tools -> preference -> editor -> use 4 spaces), but the default should represent the most widely used workflow.
Author: googlecolab
🌐
Alex Kondov
alexkondov.com › indentation-warfare-tabs-vs-spaces
Tabs or Spaces | Alex Kondov
Pressing the tab button on the keyboard outputs a tabulation character. It is interpreted by the editor as an amount of white space, usually 4 or 8 spaces. Files that are formatted with tabs are usually smaller, since a single tab character represents multiple whitespaces.
🌐
The Lucky Penny Blog
theluckypennyblog.com › how-many-spaces-is-a-tab
How Many Spaces is a Tab: A Coding Guide
It is particularly common in languages like Python, where the community’s official style guide (PEP 8) explicitly recommends using 4 spaces per indentation level to promote a clear visual structure that is easy on the eyes.
🌐
JetBrains
youtrack.jetbrains.com › issue › PY-26096 › Tabs-should-default-to-8-spaces
Tabs should default to 8 spaces : PY-26096
Our website uses some cookies and records your IP address for the purposes of accessibility, security, and managing your access to the telecommunication network. You can disable data collection and cookies by changing your browser settings, but it may affect how this website functions.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python Indentation Rules | note.nkmk.me
July 28, 2023 - – Style Guide for Python Code | peps.python.org · Many Python-supporting editors and IDEs insert four spaces when the Tab key is pressed, eliminating the need to press the space bar four times.
🌐
LearnPython.com
learnpython.com › blog › indentation-python
What 'inconsistent use of tabs and spaces in indentation' Means in Python and How to Fix It | LearnPython.com
January 30, 2023 - This could be a problem if the program is developed in different editors; after indenting with a tab in one editor, the same tab may be a different length in other editors. A space, on the other hand, only takes up one character and avoids this potential problem. This preference for spaces over tabs is reflected in the recommendation provided in the PEP 8 – Style Guide for Python Code.
🌐
Ben Mezger
seds.nl › notes › tabs_vs_spaces
Tabs vs. spaces in byte size • Ben Mezger
June 19, 2026 - Using Python and C repositories was ideal, given Linux uses 8 tabs by default and the Python community generally sticks with 4 spaces. The preference of tab vs. spaces in a codebase is a topic for another day. Today’s topic is how many bytes a tab differs from a space?
🌐
Sololearn
sololearn.com › en › Discuss › 1779272 › python-what-is-an-indentation-what-is-a-white-space-how-many-spaces-do-we-use
Python. What is an indentation? What is a white space? ...
April 29, 2019 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.