Your Python code looks absolutely perfect to you. You’ve written the logic, double-checked the variables—and then you hit ‘Run’. Immediately, Python threw this error: IndentationError: unexpected indent. But you hadn’t even touched the indentation!
IndentationError: unexpected indent is one of the most confusing errors for Python learners—because this error is often invisible to the naked eye. An extra space, a misplaced tab, or code copied from the internet can instantly trigger it.
The good news is that, once you understand why Python is so strict about indentation, fixing this error becomes quick and easy. This guide covers all 7 reasons—along with their precise solutions—and includes clear code examples that you can try out immediately.
SyntaxError: invalid syntax — 10 Quick Fixes That Actually Work
What is IndentationError: unexpected indent?
Definition: IndentationError: unexpected indent occurs when Python finds a line of code that is indented more than it should be — and there is no logical reason for that extra indentation.
Unlike most programming languages that use curly braces {} to define code blocks, Python uses indentation (spaces or tabs) to understand the structure of your code. If any line has extra or inconsistent indentation, Python cannot parse the code and raises this error before running a single line.
Here is what the full error message looks like in your terminal:
File "script.py", line 3
print("How are you?")
^
IndentationError: unexpected indent
The caret ^ points directly to the line with the extra indentation. Unlike SyntaxError, IndentationError is much more precise — it almost always points to the exact problem line.
Why Does This Error Occur?
Here are all the common reasons Python raises the Python IndentationError:
- Extra spaces have been added before the line that should be at the top level.
- Mixing tabs and spaces in the same file
- Code copied from websites, PDFs, or Stack Overflow that contains hidden characters.
- Inconsistent indentation inside functions or loops
- Code accidentally indented after a comment
- Wrong indentation inside an
if-elseortry-exceptblock - VS Code or IDLE auto-indent adding extra spaces unexpectedly
7 Quick Fixes for IndentationError: unexpected indent
1. Remove Extra Spaces From Top-Level Code
The most common cause of unexpected indent in Python is a line of code that has spaces before it when it shouldn’t. Top-level code — code that is not inside any function or loop — must start at column zero with no spaces.
Wrong:
name = "Pradeep"
print(name) # Extra 4 spaces — not inside any block
Fixed:
name = "Pradeep"
print(name) # No extra spaces — top level code
Go through your code line by line and make sure any line outside a function or loop starts at the very beginning — no spaces, no tabs.
2. Inconsistent Indentation Inside Functions
Every line inside a function must be indented at the same level. If one line has 4 spaces and another has 8 spaces—without any nested blocks—Python immediately raises a Python IndentationError.
Wrong:
def greet():
print("Hello")
print("Welcome") # 8 spaces — inconsistent!
Fixed:
def greet():
print("Hello")
print("Welcome") # 4 spaces — consistent!
All lines at the same level inside a block must have exactly the same number of spaces — always use 4 spaces as your standard.
3. Never Mix Tabs and Spaces
This is the trickiest cause of all indentation errors in Python. Both tabs and spaces appear as whitespace—yet Python treats them very differently. Mixing the two within the same file almost invariably results in an IndentationError.
Wrong:
def add(a, b):
return a + b # TAB used here
print("Done") # SPACES used here — conflict!
Fixed:
def add(a, b):
return a + b # 4 spaces
print("Done") # 4 spaces — consistent
4. Indentation After Copy-Pasting Code
This is a solution that hardly any other guide mentions. When you copy code from websites, Stack Overflow, PDFs, or even ChatGPT, the pasted code often brings along hidden tab characters or inconsistent spacing.
These invisible characters look exactly like normal spaces—but Python treats them differently, which immediately triggers an IndentationError (unexpected indent); this is a common issue associated with copy-pasting in Python.
- Paste your copied code into VS Code.
- Press
Ctrl + Ato select all code. - Press
Ctrl + Shift + P→ type “Convert Indentation to Spaces” → Enter. - Then press
Shift + Alt + Fto auto-format the entire file. - Run your code again — the error should be gone.
5. Indentation Inside if-else and try-except Blocks
Within if-else and try-except blocks, every branch must adhere to the same indentation rules. If an else or except block is not properly aligned—or if there is excessive indentation within a specific branch—Python raises an ‘unexpected indent’ error following the if statement.
Wrong:
if x > 5:
print("Greater")
print("Much greater") # Extra indent — wrong!
else:
print("Smaller")
Fixed:
if x > 5:
print("Greater")
print("Much greater") # Same level — correct
else:
print("Smaller")
6. Indented Code After a Comment
This is a beginner’s mistake that is easy to overlook. If you add a comment line and accidentally indent the next line, Python interprets it as a new block and raises an error. In Python, comments do not create new code blocks.
Wrong:
x = 10
# Print the value
print(x) # Indented after comment — wrong!
Fixed:
x = 10
# Print the value
print(x) # No indent — correct
A comment line never creates a new block. The indentation level of the line following a comment must be the same as that of the line preceding it.
7. VS Code Auto-Indent Adding Extra Spaces
Sometimes, VS Code’s auto-indent feature adds unwanted extra spaces—especially when you paste code or press Enter after a colon. This is also a known issue with the VS Code REPL in Python 3.13.
- Open VS Code Settings — press
Ctrl + , - Search for “Tab Size” → set it to
4 - Search for “Insert Spaces” → make sure it is checked ON
- Search for “Detect Indentation” → turn it OFF — this stops VS Code from guessing indent style from pasted code.
- Add this to your
settings.jsonfor permanent Python indentation control:
{
"[python]": {
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.detectIndentation": false
}
}
How to Prevent IndentationError in Python
- Always use 4 spaces for indentation — never use Tab key unless your editor converts it to spaces automatically.
- Enable Show Whitespace in VS Code (
View → Render Whitespace) — it makes spaces and tabs visible so you can spot the difference instantly. - Before pasting any code from the internet — always run Convert Indentation to Spaces in VS Code first.
- Use Pylint or Flake8 — they catch indentation issues before you run the code and underline them in real time.
Best Practices
Enable 'Render Whitespace' in VS Code — go to View → Render Whitespace → All. This displays every space as a small dot and every tab as an arrow. You will immediately spot 'mixed indentation' that would otherwise remain invisible. This single setting prevents 80% of indentation errors in Python.
Install the Black code formatter (pip install black) and add it to VS Code as your default Python formatter. Every time you save a file, Black auto-fixes all indentation — you'll never have to worry about tabs vs spaces again.
Frequently Asked Questions
This means that Python has encountered a line of code containing more indentation than expected—and there is no logical reason for this extra indentation. Python uses spacing to understand the structure of your code; therefore, any extra or inconsistent spacing prevents your program from running.
A SyntaxError is a general grammar error — like a missing colon or wrong operator. An IndentationError is a specific type of SyntaxError that happens only because of wrong spacing or indentation.