Fix My Error

Stuck? Solved.

Home › Fix Errors › Python › IndentationError
IndentationError: unexpected indent

Fix: Python IndentationError

Python's most common beginner error. Here's why it happens and the fastest way to fix it.

Last updated: October 2026

⚡ The 30-second fix

Turn on "show whitespace" in your editor. VS Code: View → Render Whitespace. PyCharm: View → Active Editor → Show Whitespaces.

You'll see the problem instantly — mixed tabs (→) and spaces (·) in the same block. Select the block, delete the leading whitespace, re-indent with 4 spaces only.

Fix it once, and it never comes back — if you configure your editor to insert spaces for tabs.

What the error means

Python uses indentation to define code blocks. Unlike JavaScript or C (which use { }), Python relies on consistent whitespace. If two lines in the same block have different indentation, Python can't tell what you meant and raises IndentationError.

def greet(name):
    print("Hello")   # 4 spaces — correct
      print(name)    # 6 spaces — ❌ IndentationError

Related errors you'll see:

  • IndentationError: expected an indented block — you forgot to indent after a :
  • TabError: inconsistent use of tabs and spaces — you mixed them
  • IndentationError: unindent does not match any outer indentation level — dedent to the wrong level

Why it happens

1

You mixed tabs and spaces

This is the #1 cause. One editor inserts a tab, another inserts 4 spaces. Visually they look the same, but Python treats them as different characters.

# Line 1: tab character
# Line 2: 4 spaces
# They look identical but Python sees two different indents
2

Copy-pasting from the web

Code copied from blogs, Stack Overflow, or docs often contains non-breaking spaces ( ) or inconsistent whitespace that looks fine but breaks Python.

3

Forgot to indent after a colon

Statements ending in : (if, for, while, def, class) must be followed by an indented block.

if user.is_active:
print("Active")   # ❌ IndentationError
4

Dedent to the wrong level

When you come out of a nested block, the indentation must match exactly one of the outer levels.

def foo():
    if True:
        print("a")
      print("b")   # ❌ 6 spaces — doesn't match any level

How to fix it (by editor)

1

VS Code

# 1. Select entire file (Ctrl+A or Cmd+A)
# 2. Open Command Palette (Ctrl+Shift+P)
# 3. Run: "Convert Indentation to Spaces"
# 4. Run: "Format Document" (Shift+Alt+F)

# Configure permanently:
# Add to settings.json:
"[python]": {
  "editor.insertSpaces": true,
  "editor.tabSize": 4,
  "editor.detectIndentation": false
}
2

PyCharm

# 1. Ctrl+Alt+L to reformat
# 2. Settings → Editor → Code Style → Python
# 3. Set "Use tab character" to OFF
# 4. Set "Tab size" to 4, "Indent" to 4
3

Command line fix (any file)

# Convert tabs to 4 spaces on Linux/macOS
expand -t 4 broken.py > fixed.py

# Or with sed
sed -i 's/\t/    /g' broken.py

🛡️ How to prevent it

❓ Frequently asked questions

How do I fix Python IndentationError?

Open your editor, enable 'show whitespace' or 'render whitespace', and look at the offending line. The problem is usually mixed tabs and spaces. Select the whole block and re-indent it with 4 spaces only. Never mix tabs and spaces in Python.

Why does Python care about indentation?

Python uses indentation to define code blocks instead of curly braces or keywords like 'end'. This makes code shorter and easier to read but means inconsistent indentation is a syntax error, not a style choice.

Should I use tabs or spaces in Python?

Always use spaces. The official PEP 8 style guide recommends 4 spaces per indentation level. Tabs are technically allowed but discouraged because they cause this exact error when mixed with spaces or viewed in different editors.

What is the difference between IndentationError and TabError?

IndentationError is thrown when indentation is inconsistent within a block. TabError is a subtype thrown specifically when the file mixes tabs and spaces. Both mean the same thing — use 4 spaces and nothing else.

How do I fix indentation in VS Code?

Select the entire file (Ctrl+A or Cmd+A), open the Command Palette (Ctrl+Shift+P), and run 'Convert Indentation to Spaces'. Then select all again and run 'Format Document' or Shift+Alt+F. This fixes most Python indentation errors in one go.

🔗 Related fixes

← All Error Fixes