Your program is two lines long. You run it, and Python refuses to start:
File "main.py", line 2
print("Hello,", name)
IndentationError: unexpected indentIndentationError: unexpected indent means a line starts further to the right than Python expected. In Python, the spaces at the start of a line aren't decoration: they show which lines belong together. A line that is indented when nothing above it opened a new block confuses Python, so it stops before running anything. The fix is almost always to move that line back to the left. This article shows how to find the line, the five usual causes, and how to stop it from happening again.
Try it yourself
Save this as main.py and run python main.py:
name = input("Your name? ")
print("Hello,", name)You'll get the error above. Notice that Python didn't even ask for your name: indentation errors are found while Python reads the file, before any line runs.
What "unexpected indent" means
In Python, indentation (the spaces at the start of a line) creates blocks. A block always starts after a line that ends with a colon, like if, for, while, def or class:
if age >= 18:
print("adult") # inside the if: indented
print("done") # back outside: not indentedEvery line in the same block must start at the same position. The message says that Python found an indent it didn't expect: the line is further right, but the line above didn't end with a colon, so there's no new block for it to belong to.
Read the error in three parts:
File "main.py", line 2: the line that's indented by mistake.- The code line below it: Python prints the line so you can see it.
IndentationError: unexpected indent: the problem is the extra space at the start of that line.
Then ask: which block did I want this line to be in? Usually the answer is "the same one as the line above", and you move it left to match.
Cause 1: an accidental space or tab
The most common cause is the smallest: one stray space, often from pressing the space bar before typing, or from deleting a line above:
price = 4
total = price * 3
print(total) File "main.py", line 2
total = price * 3
IndentationError: unexpected indentOne space is enough. Python doesn't care whether it's one space or eight: anything more than the line above, without a colon, is an unexpected indent. Delete the space so total starts in the same column as price.
A single tab at the very start of a file does the same:
File "main.py", line 1
x = 1
^
IndentationError: unexpected indentThe first line of a file must always start at the left edge.
Cause 2: code copied from a website, a chat or an AI answer
When you copy code from a tutorial, a forum or an AI chat, you often copy part of a larger block. The first line lands at the left edge where you pasted it, and the rest keeps its original indentation:
def greet(name):
print("Hi", name)
print("Welcome")
greet("Sam") File "main.py", line 3
print("Welcome")
IndentationError: unexpected indentLine 3 is indented by eight spaces, but it's inside a function whose block uses four. Python sees eight and asks: which new block is this? There isn't one, so it stops.
The fix: select the pasted lines and adjust them to match the block they belong in. In VS Code and most editors, select the lines and press Shift+Tab to move them left, or Tab to move them right, four spaces at a time.
When you paste code from an AI assistant, check the indentation of the whole block before you run it, not only whether it "looks right". Our guide on learning to code with AI has more habits for working with pasted code.
Cause 3: a line meant to stay inside an if or a loop
Sometimes you want the line to be part of a block, but the line above it isn't the start of that block. Here, the programmer wanted the second print to run only when the score is high:
score = 72
print("Your score:", score)
print("Well done")This also gives unexpected indent, because print("Your score:", score) doesn't end with a colon. What's missing isn't spaces but an if:
score = 72
print("Your score:", score)
if score >= 70:
print("Well done")So before you delete the spaces, ask what you meant. If the line should run only sometimes, add the if, for or while that starts the block.
Cause 4: a line that wanders right after a block
After a block ends, the next line must go back to the indentation of the code before the block. If you go back only part of the way, you get a related error:
for n in range(3):
print(n)
print("done") File "main.py", line 3
print("done")
^
IndentationError: unindent does not match any outer indentation levelThe message is different: "unindent" means moving left. Line 3 moved left from four spaces to two, but no earlier block starts at two spaces, so Python can't tell where it belongs. Move it to the left edge (outside the loop) or to four spaces (inside the loop).
Cause 5: tabs and spaces mixed together
A tab and four spaces can look identical in your editor, but they aren't the same to Python. When one block mixes them, Python can't compare the indentation reliably:
File "main.py", line 3
print("spaces")
TabError: inconsistent use of tabs and spaces in indentationThis one has its own name, TabError, but the cause is the same family. Most editors can fix it in one go:
- VS Code: open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P) and choose "Convert Indentation to Spaces".
- Any editor: turn on "show whitespace" or "render whitespace" to see tabs as arrows and spaces as dots.
The official Python style guide, PEP 8, recommends four spaces per level. Set your editor to insert spaces when you press Tab, and the problem disappears.
The same error in your editor
Your editor often shows this problem with a red underline before you run anything, in its own words. One wording people search for is:
expected statement, found "indent" insteadMessages like this come from the editor's code checker, not from Python itself, and the exact words depend on the editor and its extensions. They point to the same line and the same fix.
What doesn't cause it
Two things look suspicious but are fine:
- Indented comments. A line like
# noteis ignored by Python, wherever it starts. - Blank lines with spaces on them. Python ignores lines that contain only whitespace.
And indentation inside a string is part of the text, not code, so it never causes this error.
Errors that look alike
IndentationError: expected an indented block: the opposite problem. A line ends with a colon, but the next line isn't indented. See expected an indented block.IndentationError: unindent does not match any outer indentation level: a line moved left, but not to a level that exists (cause 4).TabError: inconsistent use of tabs and spaces in indentation: tabs and spaces mixed in one block (cause 5).SyntaxError: invalid syntaxon anelse: ifelseis indented further than itsif, Python 3.13 reports it as invalid syntax, not an indentation error:
File "main.py", line 4
else:
^^^^
SyntaxError: invalid syntaxLine else: up with its if.
Wrong fixes that don't help
- Deleting all indentation. It removes this error and creates
expected an indented blockerrors for everyifanddef. Fix only the line Python names. - Adding spaces until it runs. Code that runs isn't always code that's right. A line moved into the wrong block will run at the wrong time, with no error at all.
- Retyping the whole file. Show whitespace in your editor instead: you'll see the stray tab or space in seconds.
A debugging walkthrough
You pasted a function from a tutorial and Python reports unexpected indent on line 12. Here's the order to check:
- Go to line 12. Turn on "render whitespace" so you can see every space and tab.
- Look at the line above. Does it end with a colon? If not, line 12 shouldn't be further right than it.
- Decide which block line 12 belongs to. Inside the function? Then match the other lines in the function. Outside? Move it to the left edge.
- Select the whole pasted block and press Shift+Tab or Tab until its first line lines up correctly. The rest moves with it.
- Run again. If a new indentation error appears a few lines later, repeat: pasted code often has several.
Practice
Predict the error before you run each one.
- A file whose first line is
print("start")(four spaces in front). for i in range(2):, thenprint(i), thenprint("again")(six spaces).if True:, thenprint("a"), then a line# just a noteandprint("b").
Answers. (1) unexpected indent on line 1: the first line must start at the left edge. (2) unexpected indent on line 3: six spaces, but the block uses four. (3) No error: it prints a and b, because the comment is ignored.
Frequently asked questions
How do I fix "unexpected indent" in Python?
Go to the line number in the message and move that line left until it lines up with the other lines in its block. If it should be in a new block, add the if, for or def line with a colon above it.
Why do I get unexpected indent when nothing looks wrong?
Usually there's an invisible tab or a single extra space. Turn on "render whitespace" in your editor, or delete the start of the line and indent it again with the Tab key.
How many spaces should I use in Python?
Four spaces per level is the standard from PEP 8. Python accepts any number, as long as every line in the same block uses the same amount.
Why does code from ChatGPT or a website give indentation errors?
When you paste part of a block, the first line loses its indentation and the rest keep theirs. Re-indent the pasted lines as one group with Tab or Shift+Tab.
Is IndentationError a SyntaxError?
Yes. IndentationError is a special kind of SyntaxError, which is why it appears before the program starts running.
A quick checklist
- Go to the line number in the message.
- Turn on visible whitespace in your editor.
- Compare with the line above: does it end with a colon?
- Move the line to match its block, or add the missing
if,forordef. - Use spaces only, four per level.
For a method that works on any error, read how to read an error message. The Python docs on indentation explain the exact rules Python follows.
