IndentationError: unexpected indent (Fix)

IndentationError: unexpected indent means Python found spaces or tabs at the start of a line where it was not expecting a new block.

This usually happens when:

  • a top-level line starts with extra spaces
  • a line is indented even though the line above it did not start a block
  • copied code contains hidden tabs or spaces

The good news is that this error is usually fast to fix. Python often points to the exact line that caused the problem.

Quick fix

Only indent a line when it belongs inside a block such as if, for, while, def, or class.

Use consistent spaces, usually 4 spaces per level.

if True:
    print("This line is correctly indented")
print("This line is not indented, so it is outside the if block")

In this example:

  • the second line is indented because it belongs to the if block
  • the third line is not indented, so it runs outside the block

If you want a refresher on how Python blocks work, see Python indentation rules and why they matter.

What this error means

Python uses indentation to define code blocks.

That means spaces at the start of a line are not just for formatting. They change the structure of the program.

An unexpected indent means Python found extra leading spaces or tabs where a new block did not make sense.

In many cases:

  • the error points to the exact problem line
  • the real cause may be on the line above it
  • a missing colon can make the next indented line fail

When this error happens

This error often appears in these situations:

  • A line is indented even though no block was started before it.
  • Top-level code starts with spaces by mistake.
  • A copied line includes hidden spaces or tabs.
  • The indentation level changes in a way that does not match the code structure.

Python expects indentation only after a block-starting line such as:

  • if
  • for
  • while
  • def
  • class
  • try
  • except
  • else
  • elif
  • finally
  • with

If you indent after a normal statement, Python raises an error.

Example that causes the error

Here is a simple script that causes IndentationError: unexpected indent:

print("Start")
    print("Hello")
print("End")

The second line starts with spaces, but there is no block above it.

Python sees:

  • a normal top-level print("Start")
  • then an indented line that does not belong to if, for, def, or another block

That causes the error.

Correct version

Remove the extra indentation:

print("Start")
print("Hello")
print("End")

Expected output:

Start
Hello
End

Here is another common example:

name = "Sam"
    age = 10
print(name, age)

This fails for the same reason: age = 10 is indented for no valid reason.

Correct version:

name = "Sam"
age = 10
print(name, age)

Common causes

The most common reasons for this error are:

  • Accidentally pressing Tab or Space at the start of a line
  • Indenting after a line that does not end with a colon
  • Pasting code from a website or document
  • Mixing tabs and spaces
  • Editing one line manually so it no longer matches nearby lines

More specifically, watch for these common mistakes:

  • Extra spaces before a top-level statement
  • Indenting a line after a statement that does not create a block
  • Copy-pasted code with hidden indentation
  • Using tabs on one line and spaces on another
  • Missing colon on the previous line

A missing colon is especially important. For example:

if 5 > 3
    print("Yes")

This code is wrong because the if line is missing :.

Python may report a syntax problem on the if line, or the next indented line may make the problem more confusing. See SyntaxError: missing colon for that case.

How to fix it

Use this simple process:

  1. Go to the line shown in the traceback.
  2. Check whether that line should really be inside a block.
  3. If it should be top-level code, remove the extra spaces.
  4. If it belongs in a block, check the line above it for a missing colon.
  5. Convert tabs to spaces and keep indentation consistent.

Example of a missing block starter:

print("Checking value")
    print("This should not be indented")

Fix:

print("Checking value")
print("This should not be indented")

Example where the line should be indented because it belongs to a block:

if 10 > 5:
    print("10 is greater than 5")

Here the indentation is correct because the if statement starts a block.

If Python says a block is missing instead of extra indentation, that is a different error. See IndentationError: expected an indented block.

Debugging checklist

When you see this error, check these things in order:

  • Look at the error line and the line before it.
  • Check for if, for, while, def, class, try, except, else, elif, finally, and with.
  • Make sure block-starting lines end with a colon.
  • Use 4 spaces per indentation level.
  • Turn on visible whitespace in your editor if available.

You can also run your file from the terminal to see the traceback clearly:

python your_script.py
python3 your_script.py

To check for syntax and indentation problems without running the script normally, you can use:

python -m py_compile your_script.py

That command is useful when you only want Python to check the file for errors.

If you are new to reading tracebacks, how to debug Python code as a beginner can help.

Tabs vs spaces

Python code is easier to manage when you use spaces only.

For beginners, the best rule is simple:

  • use 4 spaces
  • do not use tabs
  • keep every line in the same block aligned

Many code editors can automatically convert tabs to spaces. That helps prevent indentation problems.

Mixing tabs and spaces can also cause a related error: IndentationError: unindent does not match any outer indentation level.

This error is easy to confuse with other indentation and syntax errors.

IndentationError: expected an indented block

This happens when indentation is missing, not extra.

Example:

if True:
print("Hello")

Python expected the print() line to be indented.

Related page: IndentationError: expected an indented block

IndentationError: unindent does not match any outer indentation level

This happens when indentation levels do not line up correctly.

Example:

if True:
    print("A")
  print("B")

The last line does not match the expected indentation structure.

Related page: IndentationError: unindent does not match any outer indentation level

SyntaxError: missing colon

A missing colon on a line like if x == 1: can make the next indented line look wrong.

Example:

if True
    print("Hello")

Related page: SyntaxError: missing colon

FAQ

What does unexpected indent mean in Python?

It means a line starts with spaces or tabs where Python was not expecting a new indented block.

Can a missing colon cause this error?

Yes. If a line like if x == 1 is missing a colon, the next indented line may trigger an unexpected indent error.

Should I use tabs or spaces in Python?

Use spaces. The common style is 4 spaces for each indentation level.

Why does copied code sometimes cause indentation errors?

Copied code may include tabs, non-standard spaces, or extra leading whitespace that is hard to see.

See also