We’ve all been there. You type your first “Hello, World!” in Python, hit Enter with the excitement of a kid opening a bag of chips… and BOOM:
IndentationError: unexpected indent
PythonThat’s Python basically saying:
“Sorry pal, your code looks like a badly formatted Word document.” 😭
Unlike other languages where curly braces {}
do the heavy lifting, Python cares about whitespace more than your college professor cares about fonts in a term paper. If your code isn’t indented just right, Python will throw a tantrum.
So buckle up — let’s break down Python indentation like you’re five, but also like a pro. And yes, we’ll sprinkle in humor so you don’t fall asleep halfway through.
Why Does Python Use Indentation Instead of Braces?
Imagine a world where every paragraph in a novel started without spacing. Just walls of text, no breaks. You’d throw the book out after page one.
That’s how Python sees your code. Indentation = readability.
Guido van Rossum (Python’s creator, aka the benevolent coding wizard 🧙♂️) wanted Python to force developers into writing clean, readable code. So instead of saying:
if (x > 5) {
print("Hello")
}
PythonPython says:
if x > 5:
print("Hello")
PythonSee? Cleaner, leaner, and your eyes don’t bleed.
👉 If you’re totally new to Python, you might want to peek at this What is Python? guide first before we go deeper.
Python Indentation Rules
Python indentation isn’t just about aesthetics — it’s the law. Here’s what you need to know:
- Use consistent indentation (either spaces or tabs, not both).
- Default standard: 4 spaces per indentation level. (Blame PEP 8, the holy style guide of Python.)
- Blocks like
if
,for
,while
,def
, andclass
must be indented. - Nested blocks = more indentation.
Example: Proper Indentation
for i in range(3):
if i == 1:
print("Indented properly ✅")
else:
print("Still good!")
PythonExample: Improper Indentation
for i in range(3):
if i == 1:
print("Oops, wrong indent ❌")
PythonResult? IndentationError: expected an indented block.
Python doesn’t mess around.
Common Python Indentation Errors
Here are the greatest hits of indentation mistakes:
- Unexpected Indent
You indented something that didn’t need it.print("Hello") print("Oops") # Wrong
- Unindent Does Not Match Any Outer Indentation
Basically, your indents are inconsistent (mixing tabs and spaces like a mad scientist). - IndentationError: Expected an Indented Block
You forgot to indent after a block starter.if True: print("Forgot to indent") # ❌
👉 Pro Tip: Most modern editors (like VS Code, PyCharm) will fix this for you if you just stick to spaces. And if you’re still scratching your head, check out this Python Getting Started Guide to set up your environment properly.
Tabs vs Spaces
Ah yes — the Coke vs Pepsi of Python.
- Tabs: One keystroke, easy, but can cause chaos if your teammate uses spaces.
- Spaces: Universally accepted, consistent, and PEP 8’s official darling.
The Python community’s verdict? Spaces > Tabs. Even Stack Overflow’s Developer Survey confirms the majority of Pythonistas are #TeamSpaces.
So unless you want to be roasted on Reddit, go with 4 spaces.
How Many Spaces for Indentation in Python?
The golden number is 4. Always 4. Not 2, not 8, not “whatever looks good on my screen.”
PEP 8 (the style guide) says:
“Use 4 spaces per indentation level.”
PEP 8 (the style guide)
So yeah, don’t argue with the holy scrolls.
Python Indentation Best Practices
Here’s how to stay safe from the wrath of IndentationError:
- Stick to 4 spaces (set your editor to do this automatically).
- Never mix tabs and spaces (Python 3 will yell at you).
- Indent consistently for readability.
- Use linters/formatters like
black
orflake8
to auto-clean your code. - Practice: The more you write, the more natural indentation becomes.
Want to see more beginner-friendly tips? Check out Python Variables & Assigning Values where indentation plays a sneaky role too.
FAQs on Python Indentation
Why does Python even care about indentation?
To make your code readable, consistent, and not look like spaghetti 🍝.
Can I use 2 spaces?
Technically, yes. But the community might block you faster than spam bots. Stick to 4.
How do I fix an indentation error?
Make sure your editor uses spaces, not tabs, and keep everything aligned.
Does Python 2 handle indentation differently?
Nope — but also… why are you still using Python 2? It’s retired. Let it rest in peace.
Final Thoughts
Python indentation might feel like that strict teacher who checks if your notebook margins are straight. Annoying at first, but in the end, it keeps things neat, clean, and readable.
Remember: 4 spaces, always spaces, no tabs.
And the next time you see an IndentationError, don’t panic. It’s just Python giving you a gentle nudge toward cleaner code.
Now go write some beautiful, indentation-perfect Python.
[…] Speaking of beginner mistakes, check out this guide on Python Indentation errors — trust me, it’ll save you many […]