Free: 4 trials of 20 minutes, no cardStart free

Craftstack Academy
Python

Python SyntaxError: unterminated string literal (detected at line 1)

· 9 min read

You write a greeting, run the program, and Python stops before the first line runs:

Terminal
  File "main.py", line 1
    name = "Sam
           ^
SyntaxError: unterminated string literal (detected at line 1)

SyntaxError: unterminated string literal means Python found the start of a piece of text, a string, but never found where it ends. Every string opens with a quote and must close with the same kind of quote on the same line. Here the closing " after Sam is missing. The ^ points at the quote that opened the string, and (detected at line 1) tells you where Python gave up looking. This article covers the five usual causes, including the ones where the quote looks like it's there.

Try it yourself

Save this as main.py and run python main.py:

main.py
name = "Sam
print("Hello", name)

You'll get the error above. Add the missing " after Sam, and the program prints Hello Sam.

What the error means

A string literal is text written directly in your code, between quotes: "Sam", 'hello', "42". Unterminated means not finished: the string was opened but never closed.

Read the message in three parts:

  • File "main.py", line 1: the line where the string starts.
  • ^: points at the opening quote. Everything from there to the end of the line was read as text.
  • (detected at line 1): the line where Python noticed the problem. For a normal string, it's the same line, because a normal string can't continue onto the next line.

Then ask: where did I mean this text to end, and is the matching quote really there?

The older wording: EOL while scanning string literal

Python 3.10 changed this message. In Python 3.8 and 3.9, the same mistake says:

Terminal (Python 3.8)
SyntaxError: EOL while scanning string literal

EOL means "end of line": Python reached the end of the line while still reading the string. Same error, same fixes. If a tutorial or a forum answer mentions EOL, it's talking about this.

Cause 1: a missing closing quote

The example above. It's hard to spot in a longer line, especially at the end of a print:

python
print('done)
Terminal
  File "main.py", line 1
    print('done)
          ^
SyntaxError: unterminated string literal (detected at line 1)

The ^ points at the ' before done. Python read done) as text and reached the end of the line without finding a closing '. Write print('done').

The same happens when the closing quote is there but at the wrong place:

python
print("Hello", name")

Here name" opens a new string at the end of the line, and that one is never closed. Remove the stray quote: print("Hello", name).

Cause 2: an apostrophe inside single quotes

This one catches almost everyone:

python
message = 'It's a beautiful day'
Terminal
  File "main.py", line 1
    message = 'It's a beautiful day'
                                   ^
SyntaxError: unterminated string literal (detected at line 1)

To Python, the apostrophe in It's is a closing quote. So the string is 'It', then comes s a beautiful day, and the final ' opens a new string that never closes. That's why the ^ points at the end of the line this time.

Three fixes, all valid:

main.py
message = "It's a beautiful day"
quote = 'She said "hi" to me'
also = "She said \"hi\" to me"
print(message)
print(quote)
print(also)
Terminal
It's a beautiful day
She said "hi" to me
She said "hi" to me
  • Use double quotes around text that contains an apostrophe.
  • Use single quotes around text that contains double quotes.
  • Or escape the quote with a backslash, \", which means "this quote is part of the text".

Double quotes inside double quotes

The mirror case gives a different message, because the string does close, but too early:

python
print("She said "hi" to me")
Terminal
  File "main.py", line 1
    print("She said "hi" to me")
          ^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

Python read "She said " as one string, then hi as a name, then " to me" as another string. The fix is the same: use single quotes outside, or escape the inner quotes.

Cause 3: a backslash at the end (Windows paths)

The backslash \ starts an escape sequence in a string: \n is a new line, \" is a quote character. So a backslash right before the closing quote escapes it, and the string doesn't end:

python
path = "C:\Users\sam\"
Terminal
  File "main.py", line 1
    path = "C:\Users\sam\"
           ^
SyntaxError: unterminated string literal (detected at line 1); perhaps you escaped the end quote?

Newer versions of Python (this run was 3.13) add the hint perhaps you escaped the end quote?, which is exactly right. Two fixes:

main.py
path = r"C:\Users\sam"
path2 = "C:\\Users\\sam\\"
print(path)
print(path2)
Terminal
C:\Users\sam
C:\Users\sam\
  • A raw string, with r before the quote, treats backslashes as plain characters. It still can't end with a single backslash, so leave the last one off.
  • Or double every backslash: \\ means one backslash character.

Forward slashes also work for paths on Windows in Python: "C:/Users/sam/".

Cause 4: text split over two lines

A normal string can't contain a line break. If you press Enter in the middle of a long message:

python
message = "Hello, this is a long
message on two lines"
Terminal
  File "main.py", line 1
    message = "Hello, this is a long
              ^
SyntaxError: unterminated string literal (detected at line 1)

If you want one long line of text, close the string on each line and wrap both in brackets. Python joins strings that stand next to each other:

python
message = ("Hello, this is a long "
           "message on two lines")
print(message)
Terminal
Hello, this is a long message on two lines

If you want the line break in the text, use \n inside the string, or a triple-quoted string.

Cause 5: an unclosed triple-quoted string

Triple quotes, """ or ''', make a string that can span several lines. But if you forget to close it, Python reads to the end of the file looking for the end:

python
text = """First line
second line
print(text)
Terminal
  File "main.py", line 1
    text = """First line
           ^
SyntaxError: unterminated triple-quoted string literal (detected at line 3)

Here the two line numbers differ: the string starts on line 1, and Python reached the end of the file at line 3. Everything in between, including print(text), became part of the string. Close it with """ where the text should end. (In Python 3.8, this one says EOF while scanning triple-quoted string literal: EOF means end of file.)

The f-string version

In an f-string, curly brackets hold code. A missing } gives its own message:

python
print(f"Hello {name")
Terminal
  File "main.py", line 3
    print(f"Hello {name")
                       ^
SyntaxError: f-string: expecting '}'

Close the bracket: f"Hello {name}".

Errors that look alike

  • SyntaxError: '(' was never closed: a bracket is missing, not a quote. Count your ( and ).
  • SyntaxError: invalid syntax. Perhaps you forgot a comma?: often a string that closed too early (the double-quote case above).
  • NameError: name 'yes' is not defined: the opposite mistake, text with no quotes at all. See NameError: name is not defined.
  • IndentationError: unexpected indent: another error Python finds before running anything. See unexpected indent.

Wrong fixes that don't help

  • Adding a quote at the very end of the line. If the real problem is an apostrophe in the middle, you'll get a new error or text that's cut in the wrong place.
  • Switching every string to triple quotes. It hides the error by letting the string run on, sometimes over your code.
  • Removing the backslashes from a path. "C:Userssam" is a different path. Use a raw string or double the backslashes.

A debugging walkthrough

Your program reports unterminated string literal on line 14:

  1. Go to line 14 and find the quote the ^ points at.
  2. Follow the string to the right. Where should it end? Is there a quote of the same kind there?
  3. Look for quotes inside the text: an apostrophe in single quotes, a " in double quotes.
  4. Look for a backslash right before the closing quote.
  5. If the line numbers differ (detected at line 30), look for an unclosed """ near the first line.

Your editor helps too: when a string isn't closed, the rest of the line, or the rest of the file, turns the colour of text.

Practice

Predict the result of each line.

  1. print("Don't stop")
  2. print('Don't stop')
  3. folder = "D:\new_folder\"

Answers. (1) No error: prints Don't stop. (2) unterminated string literal: the apostrophe closes the string. (3) unterminated string literal ... perhaps you escaped the end quote?. And note the hidden trap: even after fixing the end, \n in \new_folder would become a line break. Use r"D:\new_folder".

Frequently asked questions

What does "unterminated string literal" mean in Python?

A string was opened with a quote but not closed with the same kind of quote on the same line. Python can't tell where the text ends.

What does "detected at line 1" mean?

It's the line where Python gave up looking for the closing quote. For normal strings, it's the line with the error. For triple-quoted strings, it can be the last line of the file.

What is "EOL while scanning string literal"?

The same error in Python 3.9 and older. EOL stands for end of line.

How do I put an apostrophe in a Python string?

Use double quotes around the text, "It's", or escape the apostrophe: 'It\'s'.

How do I write a Windows path in Python?

Use a raw string, r"C:\Users\sam", or forward slashes, "C:/Users/sam/".

A quick checklist

  1. Find the quote the ^ points at.
  2. Check that the same kind of quote closes the string on the same line.
  3. Look for apostrophes or inner quotes that close it too early.
  4. Look for a backslash right before the closing quote.
  5. For long text, close each line's string and wrap them in brackets.

For a method that works on any error, read how to read an error message. The Python documentation lists every string form in String and Bytes literals.

1 beginner error a week

One common error, what it means and how to fix it, in a short email. Free, and you can unsubscribe in any email.

Learn by writing the code yourself.

Twenty minutes with the tutor, right in your browser. No card, nothing to install.

Start the free trial
A session: the tutor's chat on the left, the code editor with a Java program and its output on the right