SyntaxError: expected ':'
Updated
The Python error "expected ':'" means a line that opens a block, such as if, for, def or class, does not end with a colon. Where the colon goes, and the fix.
What you see
for name in ["ada", "grace"]
print(name) File "/home/me/project/main.py", line 1
for name in ["ada", "grace"]
^
SyntaxError: expected ':'What it means
if, elif, else, for, while, def, class, with, try, except, finally, match and case lines must end with a colon, which says that a block follows. This for line has none.
Python 3.10 added this message. Older versions said "invalid syntax" at the same spot.
Why it happens
- A colon left off the end of a block line, as in the example.
- A semicolon or a full stop typed instead, which is easy to do on some keyboards.
- A condition that is not finished, such as
if x >with the rest on the next line.
How to fix it
Add the colon at the end of the line that opens the block:
for name in ["ada", "grace"]:
print(name)
Example: a function definition
def greet(name)
print("Hello,", name) File "/home/me/project/main.py", line 1
def greet(name)
^
SyntaxError: expected ':'| Python | Prints |
|---|---|
| 3.9 | SyntaxError: invalid syntax |
| 3.10–3.15 | SyntaxError: expected ':' |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
How each Python version words it
| Python | Prints |
|---|---|
| 3.9 | SyntaxError: invalid syntax |
| 3.10–3.15 | SyntaxError: expected ':' |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
3.9 reports "invalid syntax". 3.10 and later say "expected ':'".
Questions
Does else need a colon?
Yes, else: and try: too, even though nothing follows them on the line.
What about a dict?
A colon also separates keys from values in a dict. A missing one there gives its own message from 3.10 on: {"a": 1, "b" 2} reports "':' expected after dictionary key".
Sources
- Compound statements, Python language reference
- What's New in Python 3.10: SyntaxErrors, Python documentation
Read on October 25, 2026.