Python SyntaxError: invalid syntax — How to Find It Fast
'invalid syntax' means Python couldn't even parse your code, so nothing ran. The caret often points *after* the real mistake — which is usually a missing colon, bracket, or quote on the line above.
Python won't even start:
File "app.py", line 4
print("hello")
^
SyntaxError: invalid syntax
A SyntaxError is different from every other error: your code never ran at all. Python couldn't parse it into a program. The catch is that the line it points to is often not the broken one — a missing bracket or colon means Python only realises something's wrong on the next line. So when the flagged line looks perfectly fine, look one line up.
The #1 cause: a missing colon
Every if, for, while, def, class, else, elif, try, except, with ends its header line with a :.
def greet(name) # ❌ missing colon
print(name)
Python reaches print and reports "invalid syntax" — because the def line was incomplete. Add the colon:
def greet(name):
print(name)
Unclosed bracket, quote, or paren
An unbalanced (, [, {, or a missing quote makes Python keep reading into the next line looking for the close:
data = {"name": "Ada", "age": 25 # ❌ no closing }
print("done") # ← error points HERE
The caret lands on print, but the real fix is the missing } above. When the flagged line is innocent, scan upward for an opener without its partner. Newer Python versions say '{' was never closed and point at the opener — read that carefully.
'=' vs '==' in a condition
Assignment (=) isn't allowed in an if:
if x = 5: # ❌ use == for comparison
Use == to compare: if x == 5:.
Other frequent triggers
- Reserved words as names:
class = "A"ordef = 1—class,def,return,import, etc. are keywords. - Mixed-up quotes:
print('it's here')— the apostrophe ends the string early. Use double quotes or escape it:"it's here". - Python 2
print:print "hi"is a SyntaxError in Python 3 — it's a function now:print("hi"). return/yieldoutside a function, orawaitoutside anasync def.
How to find it in seconds
- Go to the reported line — then look at the line above it too.
- Count brackets and quotes: does every opener have a closer?
- Check the nearest block header for its
:. - A good editor underlines the real spot before you even run — syntax highlighting that "goes wrong" at a certain point often marks the unclosed quote/bracket.
The checklist
- Suspect the line above the one Python flags.
- Missing
:on anif/for/def/while/classheader — the top cause. - Unbalanced
()[]{}or an unclosed quote. =where you meant==inside a condition.- Keyword used as a variable name, or Python 2
printsyntax.
Frequently Asked Questions
Why does the SyntaxError point to a line that looks correct?
Because the real mistake — usually a missing colon, bracket, or quote — leaves the statement "open," and Python only detects the problem when it reaches the next line. Always check the line above the one it flags.
What's the most common cause of 'invalid syntax' in Python?
A missing colon at the end of a block header (if, for, while, def, class) and unclosed brackets/quotes. Both make Python fail to parse the statement.
Is 'print "hello"' valid Python?
Only in Python 2. In Python 3 print is a function, so print "hello" raises a SyntaxError — you must write print("hello").
Stop reading, start building
This pairs with a hands-on BytExplorer course — do it on your own machine and actually keep the skill.