SyntaxError: invalid syntax (Fix)

SyntaxError: invalid syntax means Python could not understand part of your code.

This is one of the most common beginner errors. It usually happens because something is missing, extra, or written in the wrong form.

The tricky part is this: Python often points to the place where it noticed the problem, not the place where the mistake actually started. That is why you should always check the highlighted line and the line just before it.

Quick fix

# Check the line Python points to and the line just before it
# Common fixes:

# 1. Add a missing colon
if x > 5:
    print(x)

# 2. Close missing quotes or brackets
name = "Alice"
items = [1, 2, 3]

# 3. Fix misspelled keywords
for i in range(3):
    print(i)

Python often shows the error where it noticed the problem, not where the mistake started. Always inspect the previous line too.

What this error means

When you see SyntaxError: invalid syntax, Python is telling you:

  • It could not understand part of your code.
  • The code breaks Python's grammar rules.
  • The program stops before it fully runs.
  • The message is broad, so you need to inspect nearby lines carefully.

If you are new to Python, it helps to review Python syntax basics.

Why it happens

Common causes include:

  • A colon is missing after if, for, while, def, class, or try
  • A quote, parenthesis, bracket, or brace is not closed
  • A keyword is misspelled
  • Operators are used incorrectly
  • Two statements are combined in an invalid way
  • Indentation changes lead to related syntax problems

Very common examples are:

  • Missing colon after an if statement
  • Unclosed string like "hello
  • Unclosed list like [1, 2, 3
  • Writing else if instead of elif
  • Adding an extra symbol that does not belong

If the problem turns out to be spacing and code blocks, see Python indentation rules.

Example that causes the error

A missing colon after an if statement is a classic cause.

Code with the error

x = 10

if x > 5
    print("x is greater than 5")

Python will raise a syntax error because the if line should end with a colon.

Correct version

x = 10

if x > 5:
    print("x is greater than 5")

Why this works

The colon tells Python that a block of code will follow.

Statements like if, for, while, def, and class need a colon at the end.

If this is your exact problem, read SyntaxError: missing colon.

Common fixes

Try these fixes near the highlighted line:

  • Add missing colons after block starters like if, for, while, def, class, try, except, and else
  • Close all quotes and brackets
  • Check for misspelled Python keywords like elif, while, and return
  • Separate statements properly
  • Remove extra symbols that do not belong
  • Look at the line above the one Python highlights

Here are a few short examples.

1. Add a missing colon

for i in range(3):
    print(i)

2. Close a string properly

name = "Alice"
print(name)

3. Close brackets properly

numbers = [1, 2, 3]
print(numbers)

4. Fix a misspelled keyword

Wrong:

x = 5

if x > 10:
    print("big")
else if x > 3:
    print("medium")

Correct:

x = 5

if x > 10:
    print("big")
elif x > 3:
    print("medium")

How to debug step by step

Use this process to find the real problem faster:

  1. Read the full error message carefully.
  2. Check the exact line with the caret marker.
  3. Check the line before it for missing punctuation.
  4. Match every opening bracket with a closing bracket.
  5. Match every opening quote with a closing quote.
  6. Run the code again after each small fix.

You can also run your file from the terminal:

python your_script.py

Or ask Python to compile it and report syntax problems:

python -m py_compile your_script.py

If you want a broader process for finding problems, see this beginner guide to debugging Python code.

Mistakes beginners often make

These mistakes often lead to SyntaxError: invalid syntax:

  • Forgetting the colon in if, for, while, def, and class
  • Using = instead of == in a condition
  • Leaving a string unfinished
  • Writing else if instead of elif
  • Using natural language instead of Python syntax

For example, this is invalid Python:

age = 18

if age is greater than 16:
    print("You can apply")

Python does not understand is greater than as a condition.

Correct version:

age = 18

if age > 16:
    print("You can apply")

Sometimes the message changes once you get closer to the real problem. Related errors include:

FAQ

Why does Python point to the wrong line?

Python points to where it noticed the syntax problem. The real cause is often on the same line or the line before it.

Is SyntaxError the same as IndentationError?

No. IndentationError is a more specific syntax-related error about spacing and code blocks.

Can a missing bracket cause invalid syntax?

Yes. Missing brackets, quotes, or braces are very common causes of this error.

How do I fix invalid syntax quickly?

Check for missing colons, unclosed quotes, unclosed brackets, and misspelled keywords near the highlighted line.

See also