SyntaxError: unterminated string literal
Exception: SyntaxErrorCategory: parse timeRaised at: before any line runs
What this error means
Python opened a string when it saw a quote and reached the end of the line still looking for the matching one. A normal string literal cannot span lines — the closing quote must be on the same line, so the parser gives up at the newline.
Since Python 3.10 the message names the problem directly: SyntaxError: unterminated string literal (detected at line 4). Older versions reported the vaguer EOL while scanning string literal, where EOL means 'end of line'. Same bug, better wording.
File "demo.py", line 4
print("Hello)
^^^^^^^
SyntaxError: unterminated string literal (detected at line 4)
Cause 1 — the closing quote is simply missing
name = "Asha
print(name)
The fix is mechanical: count the quotes. Every ' needs a partner ', every " needs a ". Editors with bracket and quote matching make this visible — when a quote is open, the rest of the file turns string-coloured, which is often the fastest way to find it.
A related trap: an apostrophe inside a single-quoted string closes it early. print('Asha's code') is parsed as the string Asha, then the junk s code, then an empty string. Use double quotes outside, or escape it:
print("Asha's code") # double quotes outside
print('Asha\'s code') # or escape the inner one
Cause 2 — a trailing backslash eats the quote
A backslash escapes the very next character. If the character after it is the closing quote, the string never ends:
path = "C:\\projects\\" # the \\" becomes a literal quote, not a close
print(path)
File "demo.py", line 1
path = "C:\\projects\\"
^
SyntaxError: unterminated string literal (detected at line 1)
Windows paths are the usual source. Use a raw string so backslashes stay literal, or forward slashes, which Python accepts everywhere:
path = r"C:\projects\" # raw string — backslashes are literal
path = "C:/projects/" # forward slashes work on Windows too
path = "C:\\projects\\" # or double every backslash
Cause 3 — a string that genuinely needs more than one line
Sometimes the text really does span lines — a message, a query, a template. Then a normal string is the wrong tool. Use a triple-quoted string, which may contain newlines:
message = """Hi Asha,
Your report is ready.
Thanks!"""
# or join the pieces explicitly, which keeps indentation under your control
message = (
"Hi Asha,\n"
"\n"
"Your report is ready.\n"
"Thanks!"
)
Note the second form: adjacent string literals separated by a newline inside parentheses are concatenated automatically, with no + and no trailing comma needed. For long messages this reads better than a triple-quoted block, because the leading whitespace of each line is not part of the string.
Why the caret often points at the wrong line
An unterminated string swallows everything after it until the parser finds a quote or gives up. That means the reported line can be far below the real mistake — the same late-reporting behaviour as an unclosed bracket:
greeting = "Hello
total = 1 + 2
print(total, greeting)
Here line 3 is flagged even though the missing quote is on line 1, because the string that opened on line 1 does not close until the " inside print(total, greeting). When the flagged line looks perfectly fine, look up.
How to debug it
- Look at the reported line and every line above it back to the last blank line. The real quote is missing somewhere in that stretch.
- Turn on syntax colouring and scan for a region that is unexpectedly string-coloured — that is the runaway literal.
- Check for a backslash immediately before a closing quote, especially in Windows paths. Switch the path to a raw string.
- Check for an apostrophe inside a single-quoted string:
Asha's,don't,it's. - If the text legitimately spans lines, use triple quotes or adjacent literals inside parentheses — do not try to stretch a single-quoted string across a newline.
Fix it interactively
These free problems practice the string-literal rules — repair them in the browser:
Practice more Python bugs →Related messages worth knowing
- SyntaxError: the parent guide — why nothing ran at all, and the other famous wordings.
- SyntaxError: invalid character in identifier — the lookalike-character cousin of this error.
- IndentationError — the other parse-time error that stops the file before line 1.
🇮🇳 Hindi में समझें
SyntaxError: unterminated string literal ka matlab hai quote khula, par band hone wala quote nahi mila — aur ek normal string ek line se aage nahi ja sakti. Teen wajah: (1) closing quote likhna bhool gaye, ya single-quoted string ke andar apostrophe aa gaya ('Asha's code'); (2) line ke aakhir me backslash hai, jo closing quote ko escape kar deta hai — Windows paths me yahi hota hai, fix hai raw string r"C:\projects\"; (3) text sach me kai lines ka hai — tab triple quotes """...""" use karo. Debug karte waqt yaad rakho: ek adhoora string aage ka poora code nigal leta hai, isliye caret aksar neeche wali line par dikhta hai jabki galti upar hoti hai.