SyntaxError: unterminated string literal
What it means
A string starts with a quote but has no closing quote before the end of the line. Python reads everything after the opening quote as text, reaches the end of the line and gives up. The line number in brackets is where it noticed.
1. The closing quote is missing
This raises the error
Traceback (most recent call last):
File "<exec>", line 1
message = "Hello, world
^
SyntaxError: unterminated string literal (detected at line 1)The fix
Hello, world
Every string needs a matching pair of quotes on the same line. The closing quote tells Python where the text ends.
2. An apostrophe inside a single-quoted string
This raises the error
Traceback (most recent call last):
File "<exec>", line 1
status = 'It's raining'
^
SyntaxError: unterminated string literal (detected at line 1)The fix
It's raining
The apostrophe in It's closed the string early, leaving a stray quote at the end of the line. Wrapping the text in double quotes lets it contain apostrophes. 'It\'s raining' works too.
3. A string split across two lines
This raises the error
Traceback (most recent call last):
File "<exec>", line 1
note = "This message is long
^
SyntaxError: unterminated string literal (detected at line 1)The fix
This message is long and goes on to a second line
A normal string must end on the line where it starts. Triple quotes let a string span several lines. You can also write \n where the line break should go.
When you see this error, check
- Count the quotes on the line Python points to: they should come in pairs.
- Make sure the closing quote is the same kind as the opening one:
'with', and"with". - Use double quotes around text that contains an apostrophe.
- A backslash right before the closing quote escapes it, as in
"C:\files\", and Python addsperhaps you escaped the end quote?. Use forward slashes instead:"C:/files/".
Related errors
All Python errors · How to read a traceback · Step through your code