TryBugHunt

SyntaxError: unterminated string literal (detected at line 1)

SyntaxError: unterminated string literal (detected at line 1) in Python means a string was opened with a quote but never closed on the same line. Older Python versions call this "EOL while scanning string literal".

Python

How do I fix SyntaxError: unterminated string literal (detected at line 1)?

SyntaxError: unterminated string literal means a string opened with a quote never closed on that line. Usually the closing quote is missing, or an apostrophe like it's ends a single-quoted string early. Add the matching closing quote, or wrap text containing ' in double quotes. Python 3.9 and older call this EOL while scanning string literal.

  1. 1. Count the quotesOn the reported line, every string needs an opening and closing quote of the same type.
  2. 2. Look for apostrophesA ' inside 'single quotes' ends the string. Use "it's done" with double quotes instead.
  3. 3. Check for trailing backslashA \ just before the closing quote escapes it. Double the backslash, or use forward slashes in paths.

What does SyntaxError: unterminated string literal (detected at line 1) mean?

A string literal is text between matching quotes, like "hello" or 'hello'. Python reads until it finds the closing quote. If it reaches the end of the line first, it reports an unterminated string. Python 3.9 and older say "SyntaxError: EOL while scanning string literal" (EOL means end of line). Both messages mean the same thing.

Why does SyntaxError: unterminated string literal (detected at line 1) happen?

1. A missing closing quote

The quote at the end was forgotten, so the string runs to the end of the line.

Breaks

print("Hello, world)

Works

print("Hello, world")

2. An apostrophe inside single quotes

The ' in it's closes the string early, and the rest of the line opens a new string that never closes.

Breaks

msg = 'it's done'

Works

msg = "it's done"

3. A string split across lines

Normal quotes cannot span lines. Use triple quotes for text on several lines.

Breaks

text = "first line
second line"

Works

text = """first line
second line"""

4. A backslash right before the closing quote

\" means a literal quote character, so the string does not end there. This hits Windows paths.

Breaks

folder = "C:\data\"

Works

folder = "C:\\data\\"   # or simply "C:/data/"

How to find it in your own code

Checklist: 1) Go to the line in the message. 2) Count the quotes: every string needs an opening and a matching closing quote of the same type. 3) If the text contains an apostrophe, wrap it in double quotes. 4) If the string ends with a backslash, double it or use forward slashes. 5) For text on many lines, use """triple quotes""".

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides