Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 540739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:16:17+00:00 2026-05-13T10:16:17+00:00

I decided, that I learn a bit of Python. The first introduction says that

  • 0

I decided, that I learn a bit of Python. The first introduction says that it uses indentation to group statements. While the best habit is clearly to use just one of these what happens if I interchange them? How many spaces will be considered equal to one tab? Or will it fail to work at all if tabs and spaces are mixed?


Please use I'm getting an IndentationError (or a TabError). How do I fix it? to close questions where OP has a TabError resulting from mixing tabs and spaces for indentation. This question is specifically about how such indentation is interpreted, not why it causes a problem or how to fix it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:16:17+00:00Added an answer on May 13, 2026 at 10:16 am

    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’.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 307k
  • Answers 307k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's 3 different approaches: Atomic update update table set tries=tries+1… May 13, 2026 at 9:33 pm
  • Editorial Team
    Editorial Team added an answer I believe that EF will on allow you to use… May 13, 2026 at 9:33 pm
  • Editorial Team
    Editorial Team added an answer I am going to assume you are using VB.NET and… May 13, 2026 at 9:33 pm

Related Questions

I've wanted to learn Ruby for some time and even started to learn a
Building a Django app on a VPS. I am not very experienced with setting
I have decided to learn (Common) Lisp a few days ago and I realize
After being frustrated in my attempts to learn the arcana that is ASP.NET, I
First a bit of background. I have been working on the MS platform for

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.