Python Indentation Error Unexpected Indent - Common Questions & Quick Fixes
Getting "IndentationError: unexpected indent" in Python? Here are the most common questions beginners ask and their immediate solutions.
Q1: What does "IndentationError: unexpected indent" actually mean? #
A: This error occurs when Python finds indentation (spaces or tabs) where it doesn't expect any. Python uses indentation to group code blocks, and when indentation appears in the wrong place, it breaks the code structure.
Example of the problem:
print("Hello")
print("This causes unexpected indent error") # Extra spaces here
Quick fix:
🐍 Try it yourself
Q2: Why do I get indentation errors when my code "looks" correct? #
A: The most common cause is mixing tabs and spaces. Even if your code looks aligned, Python treats tabs and spaces as different characters.
Problem: Your editor might show both as whitespace, but Python sees them differently.
Solution: Use only spaces (4 spaces per indentation level is Python's standard):
🐍 Try it yourself
Q3: How can I tell if I'm using tabs or spaces? #
A: Configure your text editor to show whitespace characters, or use this Python code to detect indentation type:
🐍 Try it yourself
Q4: What's the difference between "unexpected indent" and "expected an indented block"? #
A: These are opposite problems:
- "unexpected indent" = You added indentation where Python doesn't expect it
- "expected an indented block" = You forgot to indent where Python requires it
Unexpected indent example:
print("Hello")
print("Unexpected indent here") # Error!
Expected an indented block example:
if True:
print("Missing indent here") # Error!
Both fixed:
🐍 Try it yourself
Q5: How many spaces should I use for indentation? #
A: Python's official style guide (PEP 8) recommends 4 spaces per indentation level. This is the most widely accepted standard.
🐍 Try it yourself
Q6: My function definition is giving me indentation errors. What's wrong? #
A: Common issues with function indentation:
- Missing colon after function definition
- No indentation in function body
- Inconsistent indentation within the function
Wrong:
def my_function() # Missing colon
print("No indent") # Should be indented
Correct:
🐍 Try it yourself
Q7: How do I fix indentation errors in nested structures (if/for/while)? #
A: Each nesting level should be exactly 4 spaces deeper than the previous level.
🐍 Try it yourself
Q8: Can I use tabs instead of spaces? #
A: While Python allows tabs, spaces are strongly recommended. Many Python tools and style guides expect spaces, and mixing them causes errors.
If you must use tabs:
- Use tabs consistently throughout your entire file
- Never mix tabs and spaces
- Set your editor's tab size to 4
Best practice: Configure your editor to convert tabs to 4 spaces automatically.
Q9: How do I fix indentation errors when copying code from the internet? #
A: Code from different sources often has inconsistent indentation. Here's how to fix it:
🐍 Try it yourself
Q10: What editor settings help prevent indentation errors? #
A: Configure these settings in your text editor:
- Show whitespace characters - See spaces and tabs visually
- Convert tabs to spaces - Prevents mixing
- Set tab size to 4 - Matches Python standard
- Enable auto-indentation - Automatically indent new lines
- Install Python linter - Catches errors as you type
Quick test for your editor:
🐍 Try it yourself
Quick Reference: Common Error Fixes #
| Error Message | Cause | Fix |
|---|---|---|
| "unexpected indent" | Extra indentation | Remove extra spaces/tabs |
| "expected an indented block" | Missing indentation | Add 4 spaces after : |
| "unindent does not match any outer indentation level" | Inconsistent indentation | Use consistent 4-space levels |
| "inconsistent use of tabs and spaces" | Mixed indentation | Use only spaces throughout |
Emergency Fix Checklist #
When you get an indentation error:
- Find the line number in the error message
- Check line above and below for consistency
- Count spaces/tabs - should be multiples of 4
- Look for mixed tabs/spaces - convert all to spaces
- Verify colon placement after def, if, for, while, etc.
Remember: Python's indentation errors are precise - the error message tells you exactly which line has the problem!