SyntaxError: indentation error (Fix)
This page helps beginners fix Python indentation-related syntax errors. It explains what the error means, why it happens, how to find the problem line, and how to correct it safely.
Quick fix
if True:
print("Correctly indented")
Python uses indentation to define code blocks. After a line ending with a colon, indent the next line consistently.
What this error means
Python uses indentation to show which lines belong inside a block of code.
This error usually means Python expected indentation, or found indentation that does not match the surrounding code.
This often happens after statements such as:
ifforwhiledefclasstryexceptelseelifwith
In short, the code structure is unclear because spaces or tabs are missing, extra, or inconsistent.
If you are new to this, see Python indentation rules and why they matter.
Common example that causes the error
1. A line after a colon is not indented
This is one of the most common causes.
if True:
print("Hello")
Python expects the print() line to be inside the if block.
Fixed version:
if True:
print("Hello")
2. A line is indented when Python is not expecting it
Sometimes a line starts with spaces even though it should begin at the left side.
print("Start")
print("This line has extra indentation")
Fixed version:
print("Start")
print("This line has extra indentation")
If Python shows a more specific message, see IndentationError: unexpected indent.
3. Tabs and spaces are mixed in the same block
This can be hard to see because tabs and spaces may look similar in your editor.
Problem example:
if True:
print("Line with a tab")
print("Line with spaces")
The indentation looks similar, but the first indented line uses a tab and the second uses spaces.
Fixed version:
if True:
print("Line one")
print("Line two")
Use spaces only if you are a beginner. Four spaces per indentation level is the standard.
How to fix it
Use these steps to find and fix the problem quickly:
- Check the line shown in the traceback
- Also check the line above it because the real mistake is often there
- Make sure lines inside the same block use the same indentation level
- Use 4 spaces for each indentation level
- Do not mix tabs and spaces
Example: checking the line above
This code will fail:
for i in range(3):
print(i)
The error may point to the print(i) line, but the real issue is that the line after for ...: was not indented.
Correct version:
for i in range(3):
print(i)
Expected output:
0
1
2
Example: keeping the same indentation inside one block
Wrong:
def greet():
print("Hello")
print("Welcome")
The second print() line has different indentation.
Correct:
def greet():
print("Hello")
print("Welcome")
Useful commands for checking your file
Run your script normally:
python your_script.py
Or ask Python to compile it and report syntax problems:
python -m py_compile your_script.py
If you are still stuck, a step-by-step beginner guide to debugging Python code can help.
Places where beginners often see it
Beginners often run into this error in these places:
- After
ifstatements - Inside loops
- Inside functions
- When copying code from websites or documents
- After editing code in different editors
A very common example is forgetting to indent after a function definition:
def say_hi():
print("Hi")
Correct version:
def say_hi():
print("Hi")
If the problem started after copying code, retype the indentation manually instead of trusting the pasted whitespace.
How to prevent it
A few simple habits can prevent most indentation errors:
- Configure your editor to insert spaces instead of tabs
- Turn on visible whitespace if your editor supports it
- Indent code immediately after typing a colon
- Format code consistently before running it
It also helps to understand basic Python structure. If needed, review Python syntax basics explained.
When to read a more specific error page
This page covers indentation-related syntax problems in general. Sometimes Python gives a more exact error name.
Use a more specific page when needed:
- Read IndentationError: expected an indented block if Python says that exact message
- Read SyntaxError: missing colon if the block-starting line forgot
: - Read IndentationError: unexpected indent if a line is indented when it should not be
Common mistakes
These are the most common causes of this error:
- Missing indentation after a colon
- Extra indentation on a line that should start at the left margin
- Mixing tabs and spaces
- Copying code with broken whitespace
- Indenting one line in a block more or less than the others
When checking your code, compare each line in the same block carefully. Even one extra space can be enough to break the structure.
FAQ
Is this the same as IndentationError?
They are closely related. Some indentation problems appear as SyntaxError, while others appear as IndentationError.
How many spaces should I use in Python?
Use 4 spaces for each indentation level. This is the standard style.
Why does the error point to the wrong line sometimes?
The real problem is often on the previous line, especially after a line that should start a block.
Can tabs work in Python?
Tabs can appear in code, but mixing tabs and spaces causes problems. Beginners should use spaces only.