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:

  • if
  • for
  • while
  • def
  • class

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 an if, elif, or else statement.
  • You forgot : after a loop like for or while.
  • You forgot : after defining a function with def.
  • You forgot : after defining a class with class.
  • You forgot : after try, except, finally, or with.

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 ':'
Traceback (most recent call last):File "script.py", line 3, in <module>if age > 18SyntaxError: expected ':'Where it happened — file and lineWhat went wrong — the exception typeWhy — the detailed message
Read bottom-up: the if line that starts a block is missing the required colon at its end.

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:

  1. Look at the line named in the error message.
  2. Check whether that line starts a block.
  3. Add : at the end of that line.
  4. Make sure the next line is indented correctly.
  5. 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, or class.
  • 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.

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 elif or else
  • Missing colon after for or while
  • Missing colon after def
  • Missing colon after class
  • Missing colon after try, except, finally, or with
  • 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.

See also #

Press Esc to close