SyntaxError: missing colon (Fix)
Fix the Python error SyntaxError: missing colon. This page helps beginners find where the colon is missing, understand why Python needs it, and correct the code quickly.
Quick fix #
Add a colon at the end of lines that start a block, such as if, elif, else, for, while, def, class, try, except, finally, with, and match/case.
if age > 18:
print("Adult")
for item in [1, 2, 3]:
print(item)
def greet(name):
print("Hello", name)
class Person:
pass
If you add the colon, also make sure the next line is indented.
What this error means #
Python expected a colon : at the end of a statement that starts an indented block.
A block is a group of indented lines under statements like:
ifforwhiledefclass
For example:
if True:
print("This line is inside the if block")
This is a syntax error, which means Python cannot run the code until you fix it.
Why this happens #
This error usually happens when you forget : after a line that should start a block.
Common cases include:
- You forgot
:after anif,elif, orelsestatement. - You forgot
:after a loop likefororwhile. - You forgot
:after defining a function withdef. - You forgot
:after defining a class withclass. - You forgot
:aftertry,except,finally, orwith.
Sometimes Python highlights the line where it noticed the problem, but the real mistake is on the line above it.
Common places where a colon is required #
Here are some common Python statements that must end with a colon:
if condition:elif condition:else:for item in items:while condition:def my_function():class MyClass:try:except ValueError:finally:with open("file.txt") as f:
If you are learning these statements for the first time, see Python if statements explained, Python for loops explained, and Python functions explained.
Example that causes the error #
Here is a simple example with a missing colon:
age = 20
if age > 18
print("Adult")
A beginner will usually see an error like this:
File "script.py", line 3
if age > 18
^
SyntaxError: expected ':'
The problem is easy to miss because only one character is missing.
To fix it, add the colon:
age = 20
if age > 18:
print("Adult")
Output:
Adult
How to fix it #
Use these steps:
- Look at the line named in the error message.
- Check whether that line starts a block.
- Add
:at the end of that line. - Make sure the next line is indented correctly.
- Run the code again.
Example:
def greet(name)
print("Hello", name)
This is wrong because the function definition is missing a colon.
Correct version:
def greet(name):
print("Hello", name)
Output:
greet("Sam")
# Hello Sam
Another example with a loop:
for item in [1, 2, 3]
print(item)
Fix:
for item in [1, 2, 3]:
print(item)
Output:
1
2
3
Debugging tips for beginners #
When you get this error, try these simple checks:
- Read the full error message carefully.
- Check the line Python points to.
- Also check the line just above it.
- Look for lines starting with
if,for,while,def, orclass. - Make sure those lines end with
:. - Use a code editor that highlights syntax errors.
- Fix one syntax error at a time, then run the code again.
You can also run your file from the terminal:
python script.py
Or ask Python to check the file for syntax problems:
python -m py_compile script.py
If the colon is fixed but Python then complains about indentation, see IndentationError: expected an indented block or IndentationError: unexpected indent.
Related problems that can look similar #
A missing colon often leads to other confusing errors.
For example:
- After adding the colon, you may get an indentation error if the next line is not indented correctly.
- A missing parenthesis or quote can sometimes make Python point to the wrong place.
- A more general syntax mistake may be better explained by SyntaxError: invalid syntax.
If you are unsure how indentation works in Python, read Python indentation rules and why they matter.
Common mistakes #
These are the most common causes of this error:
- Missing colon after
if - Missing colon after
eliforelse - Missing colon after
fororwhile - Missing colon after
def - Missing colon after
class - Missing colon after
try,except,finally, orwith - Looking only at the highlighted line when the actual mistake is just above it
FAQ #
Where should the colon go in Python? #
Put the colon at the end of a line that starts a block, like if x > 0: or def greet():.
Why does Python need a colon? #
The colon tells Python that an indented block of code comes next.
Why is Python pointing to the wrong line? #
Syntax errors are sometimes reported on the next line, so check the line above too.
Can this happen with else? #
Yes. else must be written as else:.
Is this the same as an indentation error? #
No. A missing colon is a syntax error. But after adding the colon, you may also need to fix indentation.