Editor

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

Broken
message = "Hello, world
print(message)
Output
Traceback (most recent call last):
  File "<exec>", line 1
    message = "Hello, world
              ^
SyntaxError: unterminated string literal (detected at line 1)

The fix

Fixed
message = "Hello, world"
print(message)
Output
Hello, world

Every string needs a matching pair of quotes on the same line. The closing quote tells Python where the text ends.

Try fixing it yourself in the editor

2. An apostrophe inside a single-quoted string

This raises the error

Broken
status = 'It's raining'
print(status)
Output
Traceback (most recent call last):
  File "<exec>", line 1
    status = 'It's raining'
                          ^
SyntaxError: unterminated string literal (detected at line 1)

The fix

Fixed
status = "It's raining"
print(status)
Output
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.

Try fixing it yourself in the editor

3. A string split across two lines

This raises the error

Broken
note = "This message is long
and goes on to a second line"
print(note)
Output
Traceback (most recent call last):
  File "<exec>", line 1
    note = "This message is long
           ^
SyntaxError: unterminated string literal (detected at line 1)

The fix

Fixed
note = """This message is long
and goes on to a second line"""
print(note)
Output
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.

Try fixing it yourself in the editor

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 adds perhaps you escaped the end quote?. Use forward slashes instead: "C:/files/".

All Python errors · How to read a traceback · Step through your code