You ran your Python code and got “IndentationError: unexpected indent” — but your code looks fine to you?
This error means Python found a line that’s indented (shifted to the right) when it shouldn’t be. Unlike most programming languages, Python uses indentation (spaces or tabs at the start of a line) to define code structure, so even one extra space can cause an error.
Don’t worry — this is one of the most common beginner mistakes, and it’s easy to fix once you know what to look for.
目次
Why IndentationError: unexpected indent happens
This error is caused by inconsistent or incorrect whitespace at the start of a line. The main reasons are:
- Mixing tabs and spaces: Your code uses tabs on some lines and spaces on others — Python can’t handle the mix
- Extra spaces on a line: A line has more indentation than Python expects
- Copy-pasting code: Code copied from websites or other files often brings invisible formatting differences
Solution 1: Fix the indentation on the error line (most common fix)
Start by looking at the exact line Python is complaining about.
1. Read the error message to find the problem line
# Example error
File "sample.py", line 4
print("hello")
^
IndentationError: unexpected indentThe error tells you the file name and line number. Go to that line in your code.
2. Check for extra indentation
# ✗ "print" is indented when it shouldn't be
x = 5
print(x) # ← This line has unexpected extra spaces
# ✓ Correct — both lines at the same level
x = 5
print(x)Remove the extra spaces or tabs at the start of the line. If the error disappears when you run the code again, you’re done.
3. Make sure indented blocks are consistent
# ✗ Mixed indentation levels inside an if block
if x == 5:
print("yes")
print("confirmed") # ← One extra level of indent
# ✓ Correct — same indentation inside the block
if x == 5:
print("yes")
print("confirmed")All lines inside the same block should be indented by the same amount — typically 4 spaces.
Solution 2: Convert tabs to spaces (or vice versa)
Mixing tabs and spaces is the sneakiest cause of IndentationError because you can’t see the difference.
1. In VS Code, show invisible characters
# Open VS Code settings (Ctrl + , on Windows, Cmd + , on Mac)
# Search for "renderWhitespace"
# Set "Editor: Render Whitespace" to "all"Now you can see dots for spaces and arrows for tabs. If you see a mix of both, that’s the problem.
2. Convert all indentation to spaces
# In VS Code:
# 1. Press Ctrl + Shift + P (Windows) or Cmd + Shift + P (Mac)
# 2. Type "Convert Indentation to Spaces"
# 3. Select that optionThis converts every tab in your file to spaces. Save the file and run your code again. If the error is gone, you’re all set.
3. Set VS Code to always use spaces
# Click the "Tab Size" indicator in VS Code's bottom-right status bar
# Select "Indent Using Spaces"
# Choose "4" for Python's standard indent sizeThis prevents the problem from happening again in future files.
Solution 3: Re-type the problematic lines
If you copied code from a website or PDF, invisible characters might have come along for the ride.
1. Delete the entire problem line and retype it manually
# Instead of trying to fix the invisible characters:
# 1. Place your cursor at the start of the problem line
# 2. Select the entire line
# 3. Delete it
# 4. Type it again from scratch, using the spacebar for indentationThis eliminates any hidden characters that might be causing the error.
2. If multiple lines are affected, re-indent the whole block
# Select all the lines in the block
# Press Shift + Tab to remove all indentation
# Press Tab to re-indent properly (or use spaces)Run your code again. If it works, the copy-pasted formatting was the culprit.
If nothing works
If you still can’t find the issue:
- Use a Python linter: Tools like
pycodestylewill check your entire file for indentation problems
# Install and run the style checker
pip install pycodestyle
pycodestyle your_script.py- Start from a known-good state: Create a new file, type your code fresh (don’t copy-paste), and compare it to the original
- Tip for asking for help: When posting your code on forums, mention whether you use tabs or spaces and which editor you’re using. This helps others spot the issue faster
Summary
- IndentationError means a line has more indentation than Python expects — check the line number in the error message
- Mixing tabs and spaces is the sneakiest cause — use “Convert Indentation to Spaces” in your editor to fix it
- Code copied from websites often has hidden formatting issues — retype the line manually if nothing else works
Related articles:
- syntax-error-invalid-syntax.html (SyntaxError: invalid syntax — causes and fixes)
- vscode-japanese-encoding.html (How to fix character encoding in VS Code)













Leave a Reply