---
title: "IndentationError in Python: what it means and how to fix it"
description: "Python uses spaces to decide which lines belong together, so indentation is part of the grammar. Here is how to read each IndentationError message and fix it fast."
url: https://articles.sythra.ai/articles/python-indentationerror-fix
slug: python-indentationerror-fix
author: "vaibhavkothari"
author_url: https://articles.sythra.ai/writers/vaibhavkothari
date_published: 2026-08-26T08:26:25.097Z
date_modified: 2026-08-29T10:29:25.265Z
topics: ["Python", "Errors", "Learning"]
reading_time_minutes: 6
publisher: "Sythra"
publisher_url: https://sythra.ai
access: free
language: en
---

# IndentationError in Python: what it means and how to fix it

> Python uses spaces to decide which lines belong together, so indentation is part of the grammar. Here is how to read each IndentationError message and fix it fast.

Source: https://articles.sythra.ai/articles/python-indentationerror-fix · Author: vaibhavkothari · Published: 2026-08-26 · Reading time: 6 min · Topics: Python, Errors, Learning

In most languages, spaces are decoration. In Python, spaces are **grammar**. Python uses indentation to decide which lines belong inside an `if`, a loop, or a function. When the spacing does not match, you get an `IndentationError` — and your program stops before running a single line.

The good news: there are only four versions of this error, and each one tells you exactly what happened.

## The four messages, decoded

| Message | Plain English | Usual cause |
|---|---|---|
| `expected an indented block` | You opened a block and left it empty | Missing indented line after `if`, `for`, `def` |
| `unexpected indent` | This line is pushed in for no reason | Stray spaces at the start of a line |
| `unindent does not match any outer indentation level` | Your closing level matches nothing above | Mixed 2-space and 4-space indents |
| `TabError: inconsistent use of tabs and spaces` | Some lines use tabs, others use spaces | Copy-pasted code from the web |

## Step 1 — Read the arrow, not the whole file

Python points at the exact line:

```text
  File "main.py", line 4
    print("hello")
    ^
IndentationError: expected an indented block after 'if' statement on line 3
```

Two facts hide in that message: the **broken line** (4) and the **line that created the expectation** (3). The fix is almost always between them.

## Step 2 — Fix "expected an indented block"

You wrote a line ending in `:` and then did not indent the next line.

```python
# Broken
age = 20
if age >= 18:
print("You can vote")     # not indented
```

Every block after a colon must be indented — four spaces is the Python standard:

```python
# Fixed
age = 20
if age >= 18:
    print("You can vote")
```

Sometimes you genuinely want an empty block — while sketching out a program, for instance. Use `pass`, a keyword that means "do nothing on purpose":

```python
def send_email():
    pass    # TODO: write this later
```

## Step 3 — Fix "unexpected indent"

Here a line is indented although nothing above it opened a block:

```python
# Broken
name = "Aditi"
    print(name)      # why is this pushed in?
```

Delete the leading spaces:

```python
# Fixed
name = "Aditi"
print(name)
```

This bites hardest when pasting from a blog or a PDF, which often carry invisible leading spaces along with the code.

## Step 4 — Fix "unindent does not match any outer indentation level"

This one confuses people because the line *looks* fine. Python is saying: "you moved back out to a level that does not exist above you."

```python
# Broken — 4 spaces, then 2 spaces
def greet(name):
    if name:
        print("Hi", name)
  print("Done")        # 2 spaces matches nothing
```

Pick one indent width and use it everywhere:

```python
# Fixed — everything in multiples of 4
def greet(name):
    if name:
        print("Hi", name)
    print("Done")
```

A quick way to see the damage is to make whitespace visible. In VS Code: **View → Render Whitespace → All**. Spaces show as dots, tabs as arrows, and the mismatch becomes obvious.

## Step 5 — Kill tabs before they kill your afternoon

`TabError` appears when one line uses a Tab character and another uses spaces. They can look identical on screen and Python still refuses.

Fix it once, permanently, in VS Code:

1. Open **File → Preferences → Settings**
2. Search **Insert Spaces** and turn it **on**
3. Search **Tab Size** and set it to **4**
4. Open the broken file, press `Ctrl+Shift+P`, run **Convert Indentation to Spaces**

If you prefer the command line, let a formatter do it for the whole project:

```bash
python -m pip install black
python -m black .
```

Black reformats every file to 4-space indentation. Most indentation bugs disappear the first time you run it.

## Step 6 — Check the line *above* when nothing looks wrong

If the reported line seems perfect, look one line up. An unclosed bracket makes Python read two lines as one:

```python
# Broken — missing closing bracket on line 1
numbers = [1, 2, 3
print("done")       # error is reported here
```

Python was still reading the list when it hit `print`. Close the bracket and both lines are fine again. Unbalanced `(`, `[`, `{` and unterminated quotes all produce this "the error is one line late" effect.

## A quick self-check routine

When an `IndentationError` appears, run through this in order:

1. Read which line Python names, and which line it says opened the block
2. Is the previous line ending in `:` followed by an indented line?
3. Are all indents a multiple of 4 spaces?
4. Are there tabs? Render whitespace and look
5. Is there an unclosed bracket or quote just above?

Four out of five times it is number 3 or number 4.

## What you learned

- Indentation is syntax in Python, not style
- Each message names a specific, different mistake
- 4 spaces everywhere, never tabs, is the whole prevention strategy
- `pass` fills a block you have not written yet
- An unclosed bracket reports its error on the *next* line

```remember
# Remember this
`expected an indented block` -> a `:` line has no body under it
`unexpected indent` -> a line is indented with no reason to be
`unindent does not match` -> the closing level matches no open level
---
In Python, indentation **is** syntax. 4 spaces, never tabs, everywhere.
```


## FAQ

### What causes IndentationError in Python?

Python decides which lines belong together by counting leading spaces. The error appears when a block after a colon is not indented, when a line is indented for no reason, or when indent widths are mixed within one file.

### How do I fix "expected an indented block"?

Indent the line after your `if`, `for`, `while`, or `def` statement by four spaces. If the block is deliberately empty, put `pass` inside it.

### Should I use tabs or spaces in Python?

Use four spaces. The official PEP 8 style guide recommends spaces, and mixing tabs with spaces raises `TabError` even when the code looks correctly aligned.

### Why does my code look correctly indented but still error?

You probably mixed tab characters with spaces, or an unclosed bracket on an earlier line merged two lines together. Turn on whitespace rendering in your editor to see the difference.

### How do I fix indentation across a whole project at once?

Install Black with `python -m pip install black` and run `python -m black .` in your project folder. It rewrites every file to consistent four-space indentation.

## Next reading on Sythra Articles

- [Your first Python program: say hello in 5 minutes](https://articles.sythra.ai/articles/first-python-hello)
- [Stop memorizing Python syntax](https://articles.sythra.ai/articles/seven-python-patterns-for-ml-notebooks)
- [TypeError: 'NoneType' object is not subscriptable](https://articles.sythra.ai/articles/nonetype-not-subscriptable-fix)

## Glossary (terms defined in this article)
- **block** (basic) — A group of lines that run together, marked by indentation
- **Black** (medium) — A tool that rewrites your code into one consistent style
