SyntaxError: invalid decimal literal / unterminated string literal (tested)

SyntaxError: invalid decimal literal and unterminated string literal are tokenizer errors: Python can't split the line into valid numbers and strings. We measured six common triggers on Python 3.13.5 and their fixes.

1. invalid decimal literal: a number runs into letters

2nd_place = "Bob"

Output:

  File "/tmp/dl/b1.py", line 1
    2nd_place = "Bob"
    ^
SyntaxError: invalid decimal literal
price = 10dollars

Output:

  File "/tmp/dl/b2.py", line 1
    price = 10dollars
             ^
SyntaxError: invalid decimal literal
total = 1_000_

Output:

  File "/tmp/dl/b7.py", line 1
    total = 1_000_
                 ^
SyntaxError: invalid decimal literal

Measured on Python 3.13.5: a name can't start with a digit (2nd_place), a number can't be glued to a word (10dollars, often a missing operator or a unit left in), and an underscore separator can't end a number (1_000_). All three say invalid decimal literal.

2. unterminated string literal: the quote closes early or never

msg = 'it's done'

Output:

  File "/tmp/dl/b4.py", line 1
    msg = 'it's done'
                    ^
SyntaxError: unterminated string literal (detected at line 1)

Measured: the apostrophe in it's closes the string, and the caret points at the end of the line, not at the apostrophe that caused it. Look for the first quote that matches the opening one.

sql = "SELECT *
  FROM users"

Output:

  File "/tmp/dl/b5.py", line 1
    sql = "SELECT *
          ^
SyntaxError: unterminated string literal (detected at line 1)

Measured: a normal string can't span lines. Use triple quotes.

path = r"C:\Users\me\"
print(path)

Output:

  File "/tmp/dl/b6.py", line 1
    path = r"C:\Users\me\"
           ^
SyntaxError: unterminated string literal (detected at line 1); perhaps you escaped the end quote?

Measured: a raw string can't end with a backslash, because \" still keeps the quote from closing it. Python 3.13 adds the hint perhaps you escaped the end quote?. Windows paths ending in \ are the usual trigger.

3. The fixed versions

place_2nd = "Bob"
price = 10  # dollars
msg = "it's done"
sql = """SELECT *
  FROM users"""
path = "C:\\Users\\me\\"
total = 1_000
print(place_2nd, price, msg, sql.split()[0], path, total)

Output:

Bob 10 it's done SELECT C:\Users\me\ 1000

Frequently asked questions

What does SyntaxError: invalid decimal literal mean?

A number is directly followed by letters or a trailing underscore. We measured it for a name starting with a digit (2nd_place), 10dollars and 1_000_.

How do I fix unterminated string literal in Python?

Find where the string's quote closes early or never closes: an apostrophe inside single quotes, a line break inside a normal string, or a raw string ending in a backslash. We measured all three.

Why does the caret point at the end of the line for an unterminated string?

Python only notices at end of line that the string never closed. We measured msg = 'it's done' with the caret at the end, although the cause is the apostrophe.

Can a raw string end with a backslash in Python?

No. We measured r"C:\Users\me\" raising unterminated string literal; use a normal string with \\ or add the backslash separately.

Written with AI assistance. Measured on 2026-10-09 with Python 3.13.5 on Linux: a name starting with a digit, a number glued to a word, a trailing underscore separator, an apostrophe in a single-quoted string, a multi-line normal string, a raw string ending in a backslash, and the fixed versions. Every block was run verbatim and its output pasted from that run. PlainHeaders is our extension; everything else here is built-in Python.