Python Indentation: rules, errors and a 4-space fixer

Try an example:

Paste your code and press Check indentation. If Python cannot read the code, you get the line, the cause in plain words and a fixed example. If it can, one more press converts the indentation to 4 spaces. The code is checked in your browser and is not run.

Indentation rules

Indentation is the whitespace at the start of a line. In Python it is part of the syntax: it shows which lines belong to a block. Many other languages mark a block with braces. Python uses the indent.

A line that ends with a colon opens a block: if, for, while, def, class, with, try and their branches. The lines of the block are indented further than that line. The block ends at the first line that returns to an earlier indent.

A Python code indentation example with two levels:

def describe(n):
    if n > 0:
        print("positive")
    else:
        print("zero or negative")
    return n


describe(3)
  • Use 4 spaces per level. This is the recommendation of PEP 8, the Python style guide.
  • All lines of one block start at the same column.
  • Python accepts any width, as long as it is the same within a block: 2 spaces and 8 spaces both work. 4 is the convention, not a requirement of the language.
  • Every indented block Python reads must hold at least one statement, and a comment does not count. Where there is nothing to do yet, write pass.
  • Blank lines and lines that hold only a comment do not open or close a block. Python ignores their indentation.
  • Inside parentheses, brackets and braces a statement can continue on the next line, and Python does not check the indentation of the continued lines. PEP 8 suggests aligning them with the opening bracket or indenting them one more level.

How to indent in Python

Start each line of a block 4 spaces further than the line with the colon. To indent several lines at once, select them and press Tab. Shift+Tab moves them back. The editor on this page works this way and inserts 4 spaces.

Indentation error in Python

A Python indentation error is reported as IndentationError or TabError. Both are kinds of SyntaxError: Python finds them while it reads the code, before any line runs. That is why a program with one badly indented line does not start at all.

This page explains 4 messages. Each card shows the message, code that produces it, the cause, and the fixed code.

IndentationError: unexpected indent

Python says: IndentationError: unexpected indent

A line starts further to the right than the line above it, but the line above does not open a block. Python allows a deeper indent only after a line that ends with a colon, such as an if, for, while or def line.

Move the line back so that it starts at the same column as the line above. If the line was meant to be inside a block, look at the line that should open the block: it must end with a colon.

  • A space or a tab at the start of the first line of the file.
  • Code pasted from a web page or a chat together with its old indent.
  • A line that was moved out of a loop or a function but kept its indent.
Example with this error
x = 1
    y = 2
print(x, y)
Fixed
x = 1
y = 2
print(x, y)

IndentationError: expected an indented block

Python says: IndentationError: expected an indented block after 'if' statement on line 2

A line that ends with a colon opens a block, and a block needs at least one line that is indented further than that line. Python found the colon, but the next line is not indented. The message names the statement and the line it is on.

Indent every line of the block by 4 spaces more than the line with the colon. If the block has nothing to do yet, write pass in it.

  • The body of an if, for, while, def or class typed without an indent.
  • A block that holds only a comment. A comment is not a statement: add pass.
  • A colon at the end of the last line of the file, with nothing after it.
Example with this error
x = 5
if x > 3:
print("big")
Fixed
x = 5
if x > 3:
    print("big")
  • Python 3.9 and older: IndentationError: expected an indented block

IndentationError: unindent does not match any outer indentation level

Python says: IndentationError: unindent does not match any outer indentation level

A line is indented less than the line above it, so it closes a block. Its indent must be the same as the indent of a block that is still open, and it is not. Python cannot tell which block the line belongs to.

Give the line exactly the same indent as the other lines of the block it belongs to. The usual cause is mixed widths: 4 spaces on one line and 2 or 3 on another.

  • Indents of different width in one file, for example after pasting code from another source.
  • A line that closes a block but stops one or two spaces short of the outer level.
Example with this error
for i in range(3):
    print(i)
  print("done")
Fixed
for i in range(3):
    print(i)
print("done")

TabError: inconsistent use of tabs and spaces in indentation

Python says: TabError: inconsistent use of tabs and spaces in indentation

The indentation mixes tabs and spaces in a way that makes the meaning of the code depend on how wide a tab is. On screen a tab and several spaces can look the same. Python does not guess the width and stops.

Use spaces only, 4 per level. Replace each tab at the start of a line with 4 spaces, and set the editor to insert spaces when you press Tab.

Example with this error
total = 0
for n in [1, 2, 3]:
	total += n
        print(total)
Fixed
total = 0
for n in [1, 2, 3]:
    total += n
    print(total)

Tabs or spaces

Use spaces. PEP 8 names spaces as the preferred way to indent and keeps tabs for code that is already indented with tabs.

A file indented only with tabs is valid Python. The trouble starts when tabs and spaces meet in one block. Python counts a tab as a jump to the next multiple of 8 columns, while an editor may draw it 4 columns wide, so the code on the screen is not the code Python reads. Python does not guess: when the structure would depend on the width of a tab, it stops with TabError.

total = 0
for n in [1, 2, 3]:
	total += n
        print(total)

In this example the third line starts with a tab and the fourth with 8 spaces. They can look aligned or not, depending on the editor. Python refuses to choose.

To see what is really there, press Show tabs and spaces below the editor. The tool marks every space and every tab at the start of each line. Then replace each tab with 4 spaces, and set your editor to insert spaces when you press Tab.

What the Python indentation fixer can and cannot fix

If Python can read the code, Convert to 4 spaces rewrites tabs and any other width (2 spaces, 3, 8, a different width in each block) to 4 spaces per level. Only the whitespace at the start of the lines changes. Quotes, commas, spacing inside a line and line breaks stay as they are.

  • Lines continued inside brackets are aligned again together with the rest of the code.
  • Text inside a docstring moves together with the code. Other multi-line strings are left as they are.
  • Before the result is shown, the page compares the structure of the code before and after the conversion. If they differ, the code is left as it is.

If Python cannot read the code (IndentationError or TabError), the tool does not guess where a block should end. It shows the line and the reason, and you fix that line by hand.

total = 0
for n in [1, 2, 3]:
    total += n
  print(total)

Should the last line run once after the loop or three times inside it? Both are valid programs with different output. Only the author knows which one was meant, and a tool that guesses can silently change what the program does.

To rewrite the whole style of the code (quotes, spacing, line length), use the Python Formatter.

FAQ

What is indentation in Python?

The spaces at the start of a line. Python uses them to tell which lines belong to a block, such as the body of an if statement or of a function.

How many spaces should I use to indent Python code?

4 per level. PEP 8 recommends it. Python itself accepts any width that stays the same within a block.

What does "unexpected indent" mean?

An unexpected indent Python error means that a line is indented further than the line above it, and the line above does not open a block. Remove the extra indent.

What does "expected an indented block" mean?

A line ends with a colon and the next line is not indented. Indent the body of the block, or write pass if the block is empty.

What does "unindent does not match any outer indentation level" mean?

A line closes a block, but its indent is not equal to the indent of any block that is still open. Line it up with the block it belongs to.

What does "inconsistent use of tabs and spaces in indentation" mean?

The indentation mixes tabs and spaces, and how deep a line is depends on the width of a tab. Replace the tabs with spaces.

Is IndentationError a syntax error?

Yes. IndentationError is a subclass of SyntaxError, and TabError is a subclass of IndentationError. Python reports them before the program starts.

Can this tool fix my IndentationError?

Not automatically. It shows the line and explains the cause. Code that Python can read is converted to 4 spaces.

Does the conversion change what my code does?

No. Only the whitespace at the start of the lines changes, and the page compares the structure of the code before and after.

Does the tool run my code?

No. It only parses the code, the way Python does before it runs a program.

Is my code uploaded?

No. The check happens in your browser.

Which Python version does it use?

Python 3.14. Python 3.9 and older word one of the messages differently, and its card shows that wording.