IndentationError in Python — What It Means and How to Fix It

What Python's IndentationError exception means and how to resolve it.

Published October 27, 2026

IndentationError is raised when code has inconsistent or incorrect indentation, which Python relies on to define code blocks.

Key points

  • Catch it specifically with "except IndentationError:" when you want to handle this exact case rather than a bare except

Example

if True:
print('bad')  # IndentationError: expected an indented block
More Python articles