Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

What is Unexpected Indent in Python



In Python, indentation is used to define the structure and flow of code. Unlike many other programming languages that use braces to define code blocks, Python relies on indentation. If the indentation is incorrect or inconsistent, Python will throw an IndentationError, specifically an unexpected indent error.

In this article, we will understand what the unexpected indent error is, how it occurs, and how to fix it.

Understanding Indentation in Python

In Python, indentation is used to define the boundaries of code blocks, such as loops, conditionals, functions, and classes. Python uses indentation levels (spaces or tabs) to define code blocks, unlike other languages that use on braces { } to mark the start and end of a block.

For example -

if condition:
    print("Condition is True")

In the above code, the print() statement is part of the if block and is indented. The indentation signals that this statement belongs to the if condition.

What Causes the unexpected indent Error?

The unexpected indent error occurs when the Python interpreter detects an indentation that is not expected in a particular context. It usually happens when -

  • The indentation is inconsistent (mixing tabs and spaces).
  • The code is indented more than expected.
  • The code is indented less than expected (creating a block that is incomplete).

Example: Mixing Tabs and Spaces

In the following example, we are mixing spaces and tabs to indent the code, which causes an unexpected indent error -

code = (
   "if True:\n"
   "\tprint('This line uses a tab')\n"
   "    print('This line uses spaces')"
)
exec(code)

In this example, the first print statement uses a tab for indentation, and the second one uses spaces. This inconsistency will cause the Python interpreter to throw an unexpected indent error -

Traceback (most recent call last):
  File "/home/cg/root/68237f533b155/main.py", line 6, in <module>
    exec(code)
  File "<string>", line 3
    print('This line uses spaces')
^
IndentationError: unindent does not match any outer indentation level

Example: Indenting More Than Expected

In this example, the second print() is indented more than expected, which will raise an unexpected indent error. The second print() should have the same level of indentation as the first one -

def greet(name):
    print("Hello")
        print("How are you?")

We get the following error -

IndentationError: unexpected indent

Example: Indenting Less Than Expected

In the following example, the indentation is missing for the block after the if statement -

if condition:
print("This is a missing indent")

Since the second line is not indented, the Python interpreter expects the block to continue with an indented line, raising an unexpected indent error.

IndentationError: expected an indented block after 'if' statement on line 1

How to Fix the unexpected indent Error?

To fix this error, you should -

  • Ensure consistent use of either tabs or spaces for indentation (spaces are recommended by PEP 8, Python's official style guide).
  • Make sure that all code blocks are indented at the right level.
  • In case of indentation problems, use an IDE or text editor that highlights indentation issues.

Example: Correcting Indentation with Spaces

Here is how to correct the indentation problem from the previous examples -

if True:
    print("This line uses spaces")
    print("This line also uses spaces")

Now, the code is properly indented with spaces, and Python will not throw an unexpected indent error -

This line uses spaces
This line also uses spaces

Using an IDE or Text Editor

Many IDEs (Integrated Development Environments) or text editors can automatically detect and fix indentation problems. They also provide features such as -

  • Highlighting mixed tabs and spaces.
  • Automatically converting tabs to spaces (or vice versa).
  • Indenting code correctly when using loops, conditionals, and functions.

For example, in Visual Studio Code, you can enable the Python: Convert Indentation to Spaces feature, which will automatically fix indentation problems in your code.

The editor will also highlight inconsistent indentation, helping you identify and fix errors quickly.

Updated on: 2025-05-14T15:00:39+05:30

525 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements