Skip to content
Happy Programming Guide
Start learning
Python

Quick Fixes for Python EOL While Scanning String Literal

This SyntaxError means a quote was opened and never closed. Here are the five causes — including the backslash trap on Windows paths — and how to fix each.

An open laptop on a wooden desk

SyntaxError: EOL while scanning string literal means Python reached the end of a line while still inside a string. A quote was opened and never closed. Newer Python versions phrase it as unterminated string literal, which is clearer, but the cause is identical. There are five ways to get there and each has a distinct fix.

Cause 1: a missing closing quote#

Python
name = "Ana
Output
SyntaxError: EOL while scanning string literal

The obvious one. In an editor with syntax highlighting, everything after the opening quote turns string-coloured and stays that way, which makes it easy to spot once you know to look.

Cause 2: mismatched quote characters#

Python
message = "Hello there'

Opened with a double quote, closed with a single. Python is still waiting for the matching double quote when the line ends. Pick one style and be consistent — most Python code uses double quotes by default.

Cause 3: an apostrophe inside single quotes#

Python
text = 'It's a fine day'

The string ends at the apostrophe in “It’s”, and then s a fine day' is nonsense to the parser. Three fixes, best first:

Python
text = "It's a fine day"        # use double quotes
text = 'It\'s a fine day'       # escape the apostrophe
text = '''It's a fine day'''    # triple quotes

The first is nearly always the right answer. Escaping works but reads badly, and once a string has several apostrophes it becomes unreadable.

Cause 4: a real newline inside a string#

Python
message = "Dear customer,
Thank you for your order."

An ordinary string cannot span lines. Three correct versions:

Python
# Escape sequence
message = "Dear customer,\nThank you for your order."

# Triple quotes keep the layout
message = """Dear customer,
Thank you for your order."""

# Implicit concatenation across lines
message = (
    "Dear customer, "
    "thank you for your order."
)

That last form is worth knowing: two string literals side by side are joined automatically, and the surrounding brackets let it span lines. It is the usual way to write a long message without a stray newline in it.

Cause 5: the backslash trap#

This is the one that catches people out, because the string looks perfectly closed:

Python
path = "C:\Users\ana\"
Output
SyntaxError: EOL while scanning string literal

The final backslash escapes the closing quote, so Python thinks the quote is part of the string and keeps looking for the real end. Any string ending in a single backslash does this.

Python
path = "C:/Users/ana/"          # forward slashes work on Windows too
path = "C:\\Users\\ana\\"          # escaped backslashes
path = r"C:\Users\ana" + "\\"      # raw string, add the slash separately

from pathlib import Path
path = Path("C:/Users/ana")     # best - no escaping at all

Why the reported line is sometimes wrong#

Python reports where it noticed the problem, not where you made it. An unclosed quote several lines earlier can produce an error on a line that looks perfectly fine.

Python
greeting = "Hello        # the mistake is here
name = "Ana"
print(greeting)          # but the error may point around here

When the reported line looks correct, look upwards. Syntax highlighting usually shows exactly where the string started running away.

Message Means
EOL while scanning string literal A single-quoted string was not closed on its line
EOF while scanning triple-quoted string literal A triple-quoted string was never closed at all
unterminated string literal The same as the first, reworded in newer Python
invalid syntax Often an unclosed bracket rather than a quote

An unclosed bracket produces a similar effect — Python keeps reading, and the error surfaces later than the mistake.

Avoiding it#

  • Use an editor with syntax highlighting. An unclosed string is visible instantly because the colour runs on.
  • Let the editor insert the closing quote. Most do this by default; type the content between them.
  • Use pathlib for paths. It removes the backslash question entirely.
  • Prefer double quotes. Apostrophes are far more common in English text than double quotes are.
  • Use triple quotes for anything multi-line. SQL, HTML fragments, long messages.

Bonus: quotes inside f-strings#

Python
name = "Ana"

print(f"Hello {name}")

# Before Python 3.12 you could not reuse the same quote inside the braces
data = {"name": "Ana"}
print(f"Hello {data['name']}")     # different quote inside - always works

Python 3.12 relaxed this and allows the same quote character inside the expression. Using a different one still works everywhere, so it is the safer habit if your code has to run on older versions.

Questions people ask#

What does EOL stand for?

End of line. Python reached the end of the line while still inside a string. EOF, in the related error, means end of file.

Why does my code fail only on Windows?

Almost always backslashes in file paths. Use forward slashes or pathlib, both of which work on every platform.

Can a string span multiple lines?

Only with triple quotes, or with a backslash at the end of the line as a continuation, or by relying on implicit concatenation inside brackets. Triple quotes are the clearest.

Does this happen in other languages?

Yes, with different wording. JavaScript says “Invalid or unexpected token”; Java says “unclosed string literal”. The underlying mistake is the same.

Where to go next#

Why is my Python code not working? A checklistRead next

Keep reading

Python

The Python Standard Library

The modules that come with Python and are worth knowing: pathlib, json, csv, datetime, collections, itertools, re, argparse and more, each with…

4 min read

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *