📱 Code Python on your phone with Python Coding - Learn PythonGet the free Android app →
  1. Home
  2. Learn
  3. Syntax errors

Guide · 3 min read

Python syntax errors explained

A syntax error means Python couldn't even understand your code, so nothing ran. The good news: almost every one comes from a short list of mistakes. Here they are, with the exact message, the broken code and the fix.

Quick answerMost Python syntax errors are a missing colon after if/for/def, wrong or mixed indentation, an unclosed bracket or quote (often reported on the next line), = used instead of ==, or curly quotes pasted from a document. Read the line number and the caret, check the line above, or paste the code into a syntax checker for a plain-English hint.

How to read a SyntaxError

  File "main.py", line 3
    if score > 50
                 ^
SyntaxError: expected ':'

Python tells you the file, the line, points a caret ^ at where it gave up, and names the problem. Python 3.10+ messages are much friendlier than older ones. If the reported line looks fine, the mistake is usually on the line before.

1. Missing colon

SyntaxError: expected ':'

Run ▸score = 72
if score > 50:          # the colon is required
    print("pass")

Every line that starts a block — if, elif, else, for, while, def, class, try, except, with — ends with a colon.

2. IndentationError

  • expected an indented block — nothing indented after a colon. Indent the body, or write pass as a placeholder.
  • unexpected indent — a line indented for no reason.
  • unindent does not match any outer indentation level — e.g. 4 spaces on one line and 3 on the next.
  • TabError: inconsistent use of tabs and spaces — configure your editor to insert 4 spaces for Tab.

Practise reading indentation with the Indent Fixer game.

3. Unclosed brackets

SyntaxError: '(' was never closed — or, in older versions, invalid syntax on the following line.

Run ▸total = sum([1, 2, 3])
print("Total:", total)    # count: every ( [ { needs its partner

4. Unclosed strings

SyntaxError: unterminated string literal — a quote is missing, or the string contains the same quote character. Use the other quote type or escape it:

Run ▸print("It's fine")          # double quotes around an apostrophe
print('She said "hi"')
print("Line one\nLine two")

5. = instead of ==

SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? — one = assigns, two compare.

6. print without brackets

SyntaxError: Missing parentheses in call to 'print' — that's Python 2 code. In Python 3, print is a function: print("hi").

7. Curly quotes and invisible characters

SyntaxError: invalid character '“' (U+201C) — text copied from Word, a PDF or a website may use “smart quotes” or non-breaking spaces. Retype the quotes in your editor.

8. Using a keyword as a name

class = "7B", for = 3 or lambda = 1 fail because those words are reserved. Pick another name: class_name.

Errors that aren't syntax errors

If the program starts running and then stops, it's a runtime error: NameError (typo or undefined variable), TypeError ("5" + 3), ValueError (int("x")), IndexError, KeyError, ZeroDivisionError. The syntax checker also flags many NameErrors before you run, and the visualizer shows the exact step where a runtime error happens.

Practise on your phone too

Python Coding - Learn Python for Android: a real Python 3 interpreter, code editor, 26 example programs and 12 marked exercises.

Get it on Google Play

Frequently asked questions

What does 'invalid syntax' mean in Python?

Python reached a point where the code isn't valid grammar. Check for a missing colon, bracket, quote or operator on that line or the line before.

Why does Python show the error on the wrong line?

An unclosed bracket or string lets Python keep reading onto the next line, so it only notices there. Look one line up.

How do I fix IndentationError: expected an indented block?

Indent the line(s) after the colon by 4 spaces, or put pass there if the block is intentionally empty.

Is there a tool that explains Python errors?

Yes — paste your code into the Python syntax checker for the line and a plain-English fix.

Keep going

Published 2026-10-22 · Updated 2026-10-22 · by the Coding Python team · code tested on Python 3