PythonMastery
reference 3 min read · lesson 54 of 80 in Errors

SyntaxError: unterminated string literal

1 · The lesson

read

What this error means

Python found an opening quote and reached the end of the line without finding the matching closing quote. An ordinary string (one in 'single' or "double" quotes) has to end on the line where it started. So Python stops there, and the file never runs.

You will also see two related messages:

  • EOL while scanning string literal is the same error in Python 3.9 and earlier. "EOL" means end of line. If a tutorial or an old server shows you that wording, everything on this page applies.
  • unterminated triple-quoted string literal means a """ or ''' string was opened and never closed anywhere in the rest of the file.

When you see it

python
  File "main.py", line 1
    message = 'Ada's invoice is ready'
                                     ^
SyntaxError: unterminated string literal (detected at line 1)
python
  File "main.py", line 1
    report_dir = "C:\Users\Ada\reports\"
                 ^
SyntaxError: unterminated string literal (detected at line 1); perhaps you escaped the end quote?

Notice where the arrows point. In the first one, the arrow is at the last quote on the line, not at the apostrophe that caused the problem. In the second, Python 3.13 makes a guess: "perhaps you escaped the end quote?" The guess is correct.

Why it happens

An apostrophe inside single quotes. Python reads 'Ada's invoice is ready' as the complete string 'Ada', followed by the bare words s invoice is ready, followed by a quote that opens a new string, and that string never closes. The apostrophe ended your string early, and the quote you meant as the closer became an opener. That is why the arrow sits at the end of the line.

A Windows path ending in a backslash. Inside a string, \" means "a literal quote character, not the end of the string". So in "C:\Users\Ada\reports\" the backslash escapes the final quote, and the string continues past the end of the line. A raw string does not fix this:

python
  File "main.py", line 1
    report_dir = r"C:\Users\Ada\reports\"
                 ^
SyntaxError: unterminated string literal (detected at line 1); perhaps you escaped the end quote?

This surprises almost everyone. Raw strings keep backslashes as they are, but a backslash still stops the next quote from closing the string, so a raw string can never end in a single backslash.

If you remove the trailing backslash in a normal string, you get the next error, because \U starts a Unicode escape:

python
  File "main.py", line 1
    report_dir = "C:\Users\Ada\reports"
                 ^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Different message, same root cause: backslashes in a Windows path mean something to Python. The fixes below deal with both.

A message split across lines. Pressing Enter in the middle of an ordinary string ends it:

python
  File "main.py", line 1
    message = "Dear Grace,
              ^
SyntaxError: unterminated string literal (detected at line 1)

A triple-quoted string with no end. Forget the closing """ on a SQL query or a docstring and Python reads to the bottom of the file looking for it:

python
query = """
SELECT id, total
FROM orders
WHERE customer = 'Grace'
print(query)
python
  File "main.py", line 1
    query = """
            ^
SyntaxError: unterminated triple-quoted string literal (detected at line 5)

Here the arrow points at the opening quotes and the "detected at" line is the end of the file. The mistake is somewhere between the two, usually just after the text you meant to quote.

How to fix it

Pick the other quote character when the text contains an apostrophe. This is the cleanest fix and the one most Python code uses:

python
message = "Ada's invoice is ready"
note = 'Grace said "ship it" at 09:40'
print(message)
print(note)

If the text contains both kinds of quote, escape the one that matches the outer quotes: 'Ada\'s "urgent" invoice'.

For Windows paths, use a raw string without the trailing backslash. You can also use forward slashes, which Windows accepts in file paths, or pathlib, which joins the pieces for you so you never need the trailing separator:

python
from pathlib import PureWindowsPath

report_dir = r"C:\Users\Ada\reports"          # raw string, no trailing backslash
same_dir = "C:/Users/Ada/reports"             # forward slashes work on Windows too
march = PureWindowsPath(report_dir) / "2026-03.csv"

print(report_dir)
print(same_dir)
print(march)

If you really need the trailing backslash in the string, double it in a normal string: "C:\\Users\\Ada\\reports\\".

For text over several lines, use triple quotes or \n:

python
letter = """Dear Grace,
Your order #1042 has shipped."""
short = "Dear Grace,\nYour order #1042 has shipped."
print(letter)
print(letter == short)

For an unterminated triple-quoted string, search the file for """ and pair them up from the top. The one without a partner is the problem. Syntax highlighting makes this quick: everything after it is shown in the string colour.

When you'd see this in real code

  • Writing a user-facing message like 'You've got mail' or 'Can't connect to the server' in a codebase that defaults to single quotes.
  • Building a folder path for a report or a backup on Windows and ending it with \ so a file name can be added to the end.
  • Pasting an address or an error message from an email into a string, line breaks and all.
  • Commenting out a block with """ and deleting only one end of it later.

See Also

Found this useful? Share it with a friend who’s learning Python.