Python is famous for being easy to read, but that readability comes from strict indentation rules. When your spacing is off, you will meet IndentationError: unexpected indent, an error that confuses many beginners because the line it names often looks perfectly fine. This guide explains what indentation means to Python, why the error appears, and the reliable ways to fix it, including the hidden tab-versus-space problem that causes most stubborn cases.
Quick Answer
An IndentationError: unexpected indent means a line is indented further than Python expects for its block. Fix it by aligning the line with the other statements in the same block, usually four spaces per level. The most common hidden cause is mixing tabs and spaces in the same file, so convert all tabs to spaces in your editor and make sure lines inside if, for, while, and def blocks line up exactly.
Why Indentation Matters in Python
Most languages use curly braces to group code. Python uses indentation instead. The spaces at the start of a line are not decoration; they tell Python which statements belong inside a loop, a condition, or a function. When a line suddenly jumps further right than the lines around it without a reason, Python cannot work out which block it belongs to and raises IndentationError.
There are three typical triggers. First, a line inside a block is indented more than its neighbours by accident. Second, code pasted from a website or PDF uses tabs while your own code uses spaces, and the mixture confuses the parser even when the lines look aligned on screen. Third, a line after a colon, such as the body of an if statement, is missing entirely, so the following line appears unexpectedly indented relative to what Python parsed.
How to Fix It Step by Step
- Read the traceback carefully. Run the script and note the file name and line number in the error. Remember that Python sometimes reports the line after the real problem, so inspect the two or three lines above it as well.
- Turn on visible whitespace in your editor. In VS Code, open Settings and enable Editor: Render Whitespace, or use the Command Palette option View: Toggle Render Whitespace. Spaces appear as dots and tabs as arrows, which instantly reveals mixed indentation.
- Convert tabs to spaces. In VS Code, click the indentation indicator in the status bar (it may say “Tab Size” or “Spaces: 4”) and choose Convert Indentation to Spaces. In other editors, use the find-and-replace or indentation conversion command to do the same.
- Re-align the block. Select the lines in the affected block and press Tab or Shift+Tab to move them together until every statement in the block starts at the same column. A block after a line ending with a colon sits one level deeper, normally four spaces.
- Check the line above for a missing statement. If you see something like
if score > 50:followed by a comment and then indented code at a deeper level, Python may complain because comments do not count as a block body. Add a real statement, or usepassas a placeholder. - Run the file again. Use
python your_script.pyfrom the terminal. If a new IndentationError appears at a different line, repeat the same checks there; a badly pasted file may have several spots to clean.
A Small Example of Wrong and Right
Here is a broken snippet where the second print statement has one extra space:
if age >= 18:
print("Adult")
print("You can vote")
Python expects both print statements at the same level because both belong to the if block. Removing the extra space from the second line fixes it:
if age >= 18:
print("Adult")
print("You can vote")
The official Python tutorial has a short, clear section on how indentation defines blocks, and it is worth reading the Python tutorial introduction to indentation once, slowly, before your next project.
Preventing Indentation Errors in Future
- Pick spaces and stay with them. Four spaces per level is the community standard described in PEP 8, and every editor can insert them automatically when you press Tab.
- Let the editor auto-indent. After typing a line ending with a colon, press Enter and let the editor place the cursor. Do not add your own spaces on top.
- Be careful with pasted code. Code from web pages, chat messages, and PDFs often carries tabs or odd spacing. Paste, then run the editor’s format command on the whole file.
- Use an automatic formatter. Tools such as Black or the built-in VS Code formatter will normalise the whole file in one keystroke; see how to set that up in our guide to formatting code automatically on save in VS Code.
- Keep blocks short while learning. Deeply nested code is harder to align. If you reach four or five levels of indentation, consider moving the inner part into its own function.
The Cousin Error: “Unindent Does Not Match”
While fixing indentation you may meet a sibling message: IndentationError: unindent does not match any outer indentation level. It means a line dedents — moves back left — to a column that no earlier block used. For example, your function body sits at four spaces, but one returning line sits at three, perhaps because it was typed with a tab that your editor displays as a different width. Python cannot match three spaces to any known level, so it complains.
The cure is the same discipline as before: make whitespace visible, convert the whole file to spaces, and re-align the block so every statement shares one of the established levels — typically 0, 4, 8, and 12 spaces for nested code. A quick way to repair one damaged function is to select its entire body, dedent it fully to the left margin with Shift+Tab, then re-indent it step by step with Tab, letting the editor apply uniform spacing. It takes less than a minute and removes invisible mixtures that no amount of squinting at the screen will reveal. After any such surgery, run the file once and skim the blocks you touched, confirming the logic still nests the way you intended: statements inside the if still inside, statements after it back at the outer level.
Conclusion
IndentationError: unexpected indent simply means a line sits deeper than the block it belongs to. Make whitespace visible, convert tabs to spaces, align every statement in a block to the same column, and check the line above the reported one for a missing body. Once your file uses consistent four-space indentation everywhere, this error becomes rare and quick to fix.
Frequently Asked Questions
Why does the error point at a line that looks correct?
Python reports where it noticed the problem, which is often the first line it could not fit into a block, not the line that caused the confusion. The real culprit is usually in the lines just above, for example an earlier line that is indented too far or a colon line with no body. Always inspect a few lines upward.
Can I mix tabs and spaces if the code looks aligned?
No. Python 3 treats inconsistent tabs and spaces as an error, and even when it runs, a tab counts differently on different editors, so alignment breaks when you share the file. Convert everything to spaces, which behave identically everywhere.
How many spaces should one indentation level be?
Four spaces is the standard recommended by PEP 8 and used by almost all Python code you will read. Two spaces will also work technically, but four is the convention that keeps your code consistent with tutorials, libraries, and team projects.
What does “expected an indented block” mean instead?
That related error means the opposite problem: after a line ending with a colon, Python found no indented statement at all. Add the body of the block, indented one level, or write pass if you plan to fill it in later.
Does my editor choice matter for indentation?
Only in convenience. Any modern editor can show whitespace, convert tabs to spaces, and auto-indent. VS Code, PyCharm, and even Notepad++ all handle this well once you enable the whitespace display and set indentation to four spaces.


Leave a Reply