Skip to content

How to Fix IndentationError: unexpected indent in Python

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix IndentationError: unexpected indent in Python. We cover key concepts, practical examples, and best practices.

Python raises IndentationError: unexpected indent when a line has extra whitespace at the start that does not match the expected indentation level of the surrounding block.

The Wrong Way

def greet(name):
    print("Hello")
    print("Welcome")  # extra space before this line

This produces:

  File "script.py", line 3
    print("Welcome")
    ^
IndentationError: unexpected indent

The Right Way

Use consistent indentation (4 spaces per level) throughout the file:

def greet(name):
    print("Hello")
    print("Welcome")

Output:

Hello
Welcome

Step-by-Step Fix

1. Reveal invisible whitespace

Run cat to show tabs as ^I:

cat -A script.py

You will see ^I for tab characters and $ at line endings.

2. Convert tabs to spaces

Replace all tabs with 4 spaces:

expand -t 4 script.py > fixed.py && mv fixed.py script.py

3. Use a formatter

Auto-format with Black:

pip install black
black script.py

4. Configure your editor

Set VS Code to use spaces for indentation:

{
    "editor.insertSpaces": true,
    "editor.tabSize": 4,
    "files.trimTrailingWhitespace": true
}

Prevention Tips

  • Enable editor.renderWhitespace in VS Code to see tabs and spaces.
  • Add Black or autopep8 to your pre-commit hooks.
  • Use a .editorconfig file to enforce consistent indentation across the team.
  • Run python3 -t script.py in CI to catch tab-space mixing early.
  • Never copy-paste code from web pages into your editor without re-indenting.

Common Mistakes with indentation error

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does Python care about indentation?

Python uses indentation to define code blocks instead of curly braces {}. This forces readable code but makes whitespace errors fatal. Every line in the same block must share the exact same indentation.

How do I fix a file that mixes tabs and spaces?

Run expand -t 4 file.py to convert all tabs to 4 spaces, then run black file.py to normalize the entire file. Never mix tabs and spaces -- configure your editor to insert spaces when you press Tab.

Does Python 3 treat tabs differently than Python 2?

Python 3 treats tabs as equivalent to 8 spaces for indentation purposes, but mixing tabs and spaces is a hard error. Python 3 disallows mixing tabs and spaces in the same file entirely. Use spaces exclusively.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro