IndentationError: unexpected indent
1 · The lesson
readWhat this error means
A line starts further to the right than Python expected. In Python, indentation is not decoration: it is how a block is opened. Only after a line that ends in a colon (if, for, def, class, with, try and similar) may the next line be indented further. Anywhere else, a deeper indent is a mistake, and Python refuses to run the file at all.
Nothing in the file has executed when you see this. It is caught while Python is reading the source, before line 1 runs.
When you see it
File "main.py", line 2 print(order_total) IndentationError: unexpected indent
File "main.py", line 5 return total IndentationError: unexpected indent
Unlike most syntax errors, Python 3.13 prints no ^ arrow here. The line number is the only clue, and for this error it is reliable: the line it names is the one that is indented too far.
Why it happens
The second traceback above comes from this function, where return is one space deeper than the line before it:
def invoice_total(lines): total = 0 for line in lines: total += line["price"] return total
One space is enough. Python compares each line with the one above. A deeper indent means "new block", and a new block is only allowed directly after a colon.
The usual causes:
- One or two extra spaces, often from editing a line quickly, or from an editor that indents by three spaces.
- A long expression split across lines without brackets (see below).
- A deleted block header. You removed an
if debug:but kept the body underneath it, still indented. - Pasted code. A snippet copied from a function, a chat message or a web page keeps its original indentation, so the first line starts four or eight spaces in, at the top level.
The split-expression case confuses even people who know Python well, because the code looks correct:
subtotal = 120 shipping = 4.99 total = subtotal + shipping + 2.50 print(total)
File "main.py", line 4 + 2.50 IndentationError: unexpected indent
Without an open bracket, a line break ends the statement. total = subtotal + shipping is complete on its own, so + 2.50 is read as a new statement, and it is wrongly indented.
Comments and blank lines never trigger it. Python ignores their indentation, so an indented # note at the top level is fine.
How to fix it
Line it up with its neighbours. If the line belongs to the same block as the line above, give it exactly the same indentation. In the function above, return total belongs after the loop, so it goes back to four spaces:
def invoice_total(lines): total = 0 for line in lines: total += line["price"] return total order = [{"price": 3.50}, {"price": 4.25}, {"price": 12.00}] print(f"Invoice total: {invoice_total(order):.2f}")
If the indent was intentional, add the header that opens the block. Indented lines need a header line above them, usually an if, for or with that is missing:
debug = True order_total = 120 if debug: print(f"order_total = {order_total}")
For a long expression, wrap it in brackets. Inside (), [] or {}, line breaks and indentation don't matter. This is the usual Python way to split a long line:
subtotal = 120 shipping = 4.99 total = (subtotal + shipping + 2.50) print(f"Total: {total:.2f}")
A backslash at the end of the line also works, but it stops working if someone adds a space after it. Brackets don't have this problem.
Let the editor do it. Select the pasted block and press Shift+Tab until it is at the right level. If you think there is one extra space somewhere, turn on visible whitespace in your editor. It is much quicker than counting.
When you'd see this in real code
- Copying the body of a function from a Stack Overflow answer and pasting it at the top of a script: every line keeps its four-space indent.
- Commenting out an
ifline during debugging and forgetting that its body is still indented underneath. - Splitting a long SQL-building expression or a chained calculation across lines without wrapping it in parentheses.
- A teammate's editor uses two-space indents and yours uses four, so one edited line ends up two spaces away from the lines around it.
Related errors
- IndentationError: expected an indented block: the opposite case, where a colon opened a block and no indented line followed.
- IndentationError: unindent does not match any outer indentation level: a line moved back to the left, but to a column where no block started.
- TabError: inconsistent use of tabs and spaces in indentation: the indentation looks right, but it mixes tabs and spaces.
- SyntaxError: invalid syntax
See Also
- All Python errors: the full index, by type and by when it happens.
- Conditionals (tutorial)
- Comments and style
- Functions (tutorial)
Found this useful? Share it with a friend who’s learning Python.