SyntaxError: unterminated string literal
1 · The lesson
readWhat 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 finish on the line it started on, so Python gives up there and the file never runs.
You will also meet two relatives of this message:
EOL while scanning string literalis 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 literalmeans a"""or'''string was opened and never closed anywhere in the rest of the file.
When you see it
File "main.py", line 1 message = 'Ada's invoice is ready' ^ SyntaxError: unterminated string literal (detected at line 1)
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 trouble. In the second, Python 3.13 has a good guess and says so: "perhaps you escaped the end quote?" It is right.
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 — which 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 final quote is swallowed by the backslash before it, and the string runs off the end of the line. A raw string does not save you here:
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?
That 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.
Fix the trailing backslash in a normal string and you hit the next wall, because \U starts a Unicode escape:
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:
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:
query = """
SELECT id, total
FROM orders
WHERE customer = 'Grace'
print(query)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:
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 — or forward slashes, which Windows accepts in file paths, or pathlib, which joins the pieces for you so you never need the trailing separator:
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:
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 culprit. Syntax highlighting makes this quick: everything after it turns string-coloured.
When you'd actually 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 tacked on. - 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.
Related errors
- SyntaxError: '(' was never closed — the same kind of mistake, with a bracket instead of a quote.
- SyntaxError: invalid syntax — the catch-all parse error.
- FileNotFoundError — what a Windows path with a stray escape like
\nor\tturns into when it does parse:"reports\new.csv"quietly contains a newline.
See Also
- All Python errors — the full index, by type and by when it happens.
- Working with Strings
- Strings cheat sheet
- File I/O (tutorial)
Found this useful? Share it with a friend who’s learning Python.