Home › Errors › IndentationError
IndentationError: unexpected indent
Exception: IndentationErrorCategory: parse timeRaised at: before any line runs
What this error means
In Python, indentation is syntax — it is how the language marks a block, since there are no braces. This error means a line is indented further than the statement before it, and no header ending in : opened a new block to justify it.
Like every SyntaxError, it is raised while the file is being compiled: nothing ran, not even line 1. The message names the file, the line and the column of the first stray character:
File "demo.py", line 3
print(name)
^
IndentationError: unexpected indent
Cause 1 — a stray indent on a plain statement
name = "Asha"
print(name)
Line 2 is indented but line 1 opened no block, so the parser has nowhere to put it. Remove the leading whitespace. The mirror case is IndentationError: expected an indented block after 'if' statement on line 3 — a header that did open a block but got nothing indented underneath.
Cause 2 — tabs mixed with spaces
A tab and a run of spaces can look identical on screen and be completely different to the parser. When the two appear in the same block you get the dedicated subclass, TabError:
File "demo.py", line 4
print(total)
^
TabError: inconsistent use of tabs and spaces in indentation
The fix is mechanical, not manual: convert the whole file to spaces. In VS Code, click Spaces: 4 in the status bar → *Convert Indentation to Spaces*. In Vim, :set expandtab then :retab. Everywhere, 4 spaces per level is the standard PEP 8 asks for — and never mix two different widths inside one block.
Cause 3 — copy-paste from a webpage, PDF or chat
Formatted code carries invisible baggage: non-breaking spaces, leading indentation from the original listing, smart quotes, and lines re-wrapped at the page width. All of them survive the paste and all of them break the parse. Paste into a plain-text buffer first, or retype the block, then check for these specifically:
- Non-breaking space (
\u00a0) instead of a normal space — common from PDFs and web pages. - A partially selected paste that kept the indentation of the source listing but lost the header above it.
- Editor auto-indent adding a second level after a
:you typed yourself — the line ends up one level too deep. - A comment indented further than its code, or code indented further than the
defit belongs to.
Cause 4 — a one-liner that lost its shape
if score > 50:
print("pass") # 8 spaces — legal, but now nothing at 4 will match
print("done") # IndentationError: unindent does not match any outer indentation level
Python does not care how many spaces you use as long as the level is consistent, but it does require every line of the same block to start at the same column. Eight spaces on one line and four on the next is two different blocks, and the second one matches nothing.
How to debug it
- Turn on invisible characters. VS Code: *View → Render Whitespace*. Vim:
:set list. Sublime: *View → Show Whitespace*. The stray space becomes visible instantly. - Look at the reported line and the line above it — the block a line belongs to is decided by the previous header.
- Run
python -tt yourfile.py, which upgrades tab/space inconsistency warnings to errors and pinpoints the offending line. - Select the whole file and re-indent it: VS Code *Format Document*, or
autopep8 --in-place yourfile.py. - For pasted code, retype the header lines by hand so the editor generates the indentation itself instead of inheriting the source's.
Fix it interactively
These free problems are indentation and block-structure bugs — repair them in the browser:
Practice more Python bugs →Related messages worth knowing
- IndentationError: the full guide — expected an indented block, unexpected indent and TabError together.
- SyntaxError — the parent exception — why nothing ran at all.
- IndentationError (Hindi) — यही guide आसान Hindi में.
🇮🇳 Hindi में समझें
Python me indentation syntax hai — block banane ka tarika. IndentationError: unexpected indent ka matlab hai koi line aage se indented hai jabki uske upar wali line ne koi block shuru hi nahi kiya (koi : khatam hone wala header nahi tha). Sabse common wajah: (1) plain statement ke aage galti se space chala gaya; (2) tabs aur spaces mix ho gaye — dono screen par same dikhte hain par parser ke liye alag hain, is case me TabError milta hai; (3) web page ya PDF se code paste kiya, jisme non-breaking spaces aa gaye. Fix: editor me whitespace dikhna on karo (VS Code: View → Render Whitespace), poori file ko spaces me convert karo, aur har level par 4 spaces use karo. Same block ki har line same column se shuru honi chahiye.