How to Fix “IndentationError: unexpected indent” in Python


If you’re new to Python, one of the first (and most frustrating) errors you’ll likely encounter is the “IndentationError: unexpected indent.” This common error can confuse beginners and even trip up experienced coders from time to time. But don’t worry—this guide will explain what causes this error and how to fix it with ease.

Let’s dive into the details of this indentation issue and learn how to write cleaner, bug-free Python code.

What Is “IndentationError: unexpected indent”?

Python uses indentation (spaces or tabs) to define the structure of the code, especially for blocks like if, for, while, functions, and classes. Unlike many other languages that use curly braces {}, Python relies on proper indentation to determine what code belongs together.

If Python encounters an unexpected indentation—a line that is more indented than it should be—it throws the following error:

IndentationError: unexpected indent

Example of the Error

def greet():
print("Hello, world!")

Output:

IndentationError: expected an indented block after function definition on line 1

Now, consider this version:

def greet():
print("Hello, world!")
print("This is indented too far")

Output:

IndentationError: unexpected indent

Python expects consistent indentation. In this example, the second print() is over-indented compared to the first, and that inconsistency triggers the error.

How to Fix “IndentationError: unexpected indent”

Here are the most effective ways to resolve this error:

1. Use Consistent Indentation (Spaces vs. Tabs)

The most common cause of this error is mixing tabs and spaces.

Fix: Choose either tabs or spaces and stick to it. According to PEP 8 (Python’s official style guide), the recommended approach is to use 4 spaces per indentation level.

If you’re using an editor like VS Code or PyCharm, enable the setting to convert tabs to spaces automatically.

2. Check for Unnecessary Indentation

Sometimes a line is indented even though it shouldn’t be.

Wrong:

x = 10
print(x)

Right:

x = 10
print(x)

Fix: Only indent inside a code block (like within a loop, condition, or function). Otherwise, no indentation is required.

3. Look Inside Copy-Pasted Code

When you copy code from websites, PDFs, or emails, it might include invisible indentation characters that cause issues.

Fix: Select all the code and re-indent it using your editor’s auto-indent feature or formatting shortcut.

4. Avoid Inconsistent Blocks

Make sure all lines within a block are aligned equally.

Wrong:

for i in range(5):
print(i)
print(i * 2)

Right:

for i in range(5):
print(i)
print(i * 2)

Fix: Ensure all statements in a block use the same indentation level—ideally 4 spaces.

5. Use Linting and Code Formatters

Tools like flake8 (a linter) and black (a formatter) can automatically help detect and fix indentation problems.

  • flake8 warns about mixed or inconsistent indentation
  • black reformats your code to follow standard style guidelines

These tools are especially useful in larger projects or team environments.

Pro Tip: Set Up Your Editor Correctly

To prevent indentation errors entirely, configure your editor with the following settings:

  • Set default indentation to 4 spaces
  • Enable “Convert tabs to spaces”
  • Use auto-format on save
  • Install Python linting extensions

Most modern editors like VS Code, PyCharm, and Sublime Text offer built-in support for these settings.

Why Python Is Strict About Indentation

Python is designed to prioritize readability and clean syntax. While its strict indentation rules might feel tedious at first, they ultimately help developers write clearer and more maintainable code.

Over time, many developers come to appreciate Python’s formatting structure, as it eliminates ambiguity and enforces best practices by design.

Final Thoughts

IndentationError: unexpected indent” is one of the most common errors encountered by Python beginners. Fortunately, it’s also one of the easiest to fix. The key is to maintain a consistent indentation style, avoid mixing tabs and spaces, and let your code editor assist you.

When in doubt, recheck your indentation and start with a clean format. With a bit of practice, you’ll overcome this error effortlessly and focus on writing better Python code.


Leave a Comment

Your email address will not be published. Required fields are marked *