ShareYourGroupShareYourGroupPython

SyntaxError: what it means and how to fix it

A SyntaxError means Python could not even read your program. The grammar of a line is broken: a bracket is not closed, a colon is missing, a word is in the wrong place. Python checks the whole file before running any of it, so with a syntax error nothing runs, not even the lines above the mistake.

How to read the message

marks = 72
if marks >= 33
    print("Pass")

What Python says

  File "example.py", line 2
    if marks >= 33
                  ^
SyntaxError: expected ':'

The message gives the line number, shows the line, and puts a small arrow under the place where Python got confused. Modern versions of Python often say what they expected, as here.

One warning: the real mistake is sometimes on the line before the one shown. A bracket left open on line 4 is only noticed when Python reaches line 5 and finds something that cannot belong inside brackets. If the marked line looks fine, look one line up.

Cause 1: a missing colon

if, elif, else, for, while, def, class, try and except lines all end with a colon. The example above shows what happens without it. The fix is to add : at the end of the line.

Cause 2: a quotation mark not closed

print("Hello)

What Python says

  File "example.py", line 1
    print("Hello)
          ^
SyntaxError: unterminated string literal (detected at line 1)

Every string needs the same kind of quotation mark at both ends. A string that starts with " must end with ".

Cause 3: a bracket not closed

total = (4 + 5
print(total)

What Python says

  File "example.py", line 1
    total = (4 + 5
            ^
SyntaxError: '(' was never closed

Count your brackets. Most editors highlight the matching bracket when the cursor sits next to one.

Cause 4: one equals sign in a comparison

x = 5
if x = 5:
    print("five")

What Python says

  File "example.py", line 2
    if x = 5:
       ^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

A single = stores a value. To compare, use ==.

Cause 5: a name that is not allowed

class = 8

What Python says

  File "example.py", line 1
    class = 8
          ^
SyntaxError: invalid syntax

class is a word Python keeps for itself. Others include for, if, def, import and return. Choose a different name such as class_number. Names also cannot start with a digit or contain spaces.

Cause 6: print without brackets

Very old tutorials, written for Python 2, show print "hello". In Python 3, print is a function and needs brackets.

print "hello"

What Python says

  File "example.py", line 1
    print "hello"
    ^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

A checklist

  1. Go to the line number in the message. Look at the arrow.
  2. Check that line, then the line above it.
  3. Look for a missing colon, an unclosed quotation mark or bracket, or = where == is needed.
  4. Fix one error and run again. One syntax error can hide others, and Python reports them one at a time.

A syntax error is always a typing problem, never a problem with your idea. That makes it the easiest kind to fix once you are used to reading the message. A related error about spaces at the start of a line has its own page: IndentationError.

Other error messages

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.