How to fix Python indentation error

Learn to Code Today!

Learn 10x faster: coding, no-code and data skills. Join millions of users mastering new tech skills and accelerating their career with Enki.
Get started

Indentation is a crucial part of Python. Unlike many other programming languages that utilize braces {} to define blocks of code, Python uses indentation. This means that how you visually structure your code with spaces or tabs is not just about readability; it's functional.

Each level of indentation represents a new block or suite. Consistency is the golden rule—typically, four spaces or a single tab denote each level of indentation.

Join Enki to learn Python and other programming languages 10x faster.

How Python's Indentation Works

In Python, statements sharing the same level of indentation are part of the same block of code and execute together. Let's look at a simple example:

Here, print("X is 1!") lies within the if statement's block due to its indentation. Meanwhile, print("End of block") represents a statement that resides outside the if block and is executed as a separate instruction once the block completes.

Tabs vs Spaces in Python

According to PEP 8, the style guide for Python code, spaces are often preferred over tabs. Using spaces creates consistency and helps in avoiding errors like mixing tabs and spaces. Mixing these can lead to an IndentationError, a common mistake developers encounter. Most modern editors can convert tabs into spaces automatically, typically using four spaces per indentation level.

Common Indentation Errors and Their Fixes

Let's review some common indentation errors and explore how to resolve them.

IndentationError: Unexpected Indent

This error occurs when Python finds an indented line somewhere it doesn’t expect one. For example:

The above code block raises an IndentationError because print("Hello!") should be indented. Here’s the corrected version:

IndentationError: Unindent Does Not Match Any Outer Indentation Level

This issue arises when lines of code within the same block have inconsistent indentation. Consider:

The error arises from inconsistent spaces before print(number). Fix it as follows:

For more guidance, see Real Python on Code Indentation.

Examples of Correct and Broken Code

To understand indentation better, observe these examples.

Proper Indentation in Nested Blocks

In this example, all blocks are properly indented with four spaces per level, ensuring smooth execution.

Example with Mixed Spacing Causing Errors

Notice the extra space before the if statement? This causes an error. Ensure consistency with either only spaces or only tabs across the entire script.

Wrapping Up

Mastery of indentation is essential in Python. It dictates code execution, enabling a visual organization of logical blocks without the need for braces or semicolons. Consistent indentation ensures readability and prevents errors, contributing to cleaner and error-free code.

For further exploration of Python best practices, check out courses on Enki.com. We are committed to enhancing your coding skills and experience.

About Enki

  • Fully personalized online up-skilling
  • Unlimited AI coaching
  • Designed by Silicon Valley experts

More articles

Meet your AI-enabled coach

Professional athletes have a coach for every aspect of their performance. Why can’t you for your work? Enki’s AI-powered coaching on-demand - combined with state of the art, structured learning content - makes this a reality.
1
1:1 AI Coaching
How do I remove duplicate emails?
Convert the list to a set and back to a list. Sets automatically remove duplicates.
2
Personalized Exercises
3
Interactive practice

Unlock full access to all skills on Enki with a 7-day free trial

Get started