HOWTO · Python

How to Fix Error - EOL While Scanning String Literal in Python

Discover how to fix the EOL while scanning string literal error in Python. This guide explains the causes and provides effective solutions, including escaping backslashes and using raw strings. Enhance your Python coding skills and eliminate frustrating errors with our easy-to-follow tips.

On this page

The legacy diagnostic EOL while scanning string literal means Python reached the end of a line while parsing a string that had not been closed. Recent Python versions may describe the same problem as an unterminated string literal, so the exact wording depends on the interpreter. A common cause is a backslash immediately before what looks like the closing quote: that backslash escapes the quote, and the parser keeps looking for the end of the string.

An unmatched opening quote or a mismatched pair of quote marks can also leave a string open. Start by checking the source line named in the error, then inspect the string’s opening and closing delimiters and any backslashes just before the closing delimiter.

What the EOL string-literal error means

Python uses quotes to mark the start and end of a string. In an ordinary string, a backslash introduces an escape sequence; \\ represents one literal backslash. A quote preceded by an odd number of backslashes is escaped, so it does not close the string. The parser can then report an end-of-line string error even though the visible line appears to contain its closing quote.

For example, the inline fragment path = "C:\logs\" leaves the quote escaped. It is intentionally shown as inline text because it is invalid Python and cannot be run as a complete example. The important distinction is between the source spelling and the value Python creates: source code needs the right number of backslashes for the value you want.

Why a raw string cannot end with a backslash

The r or R prefix tells Python to retain backslashes in the string value instead of interpreting most escape sequences. It does not remove the quote-delimiting rules. In a raw literal, a backslash can still escape a quote while Python parses the source. That is why r"C:\logs\" is invalid: its final backslash escapes the apparent closing quote, leaving the literal open.

A raw literal therefore cannot end with an odd number of backslashes. A raw literal ending with an even number can be valid, but it produces that even number of backslashes; it is not a way to write a value ending in exactly one backslash. Use a raw literal for the portion that benefits from raw spelling, then append a separately escaped backslash when needed.

Fix the string literal

For a value that ends with one backslash, either escape the final backslash in an ordinary string, or append one to a raw string that does not itself end with a backslash. These two assignments produce the actual string value C:\logs\, with one backslash before logs and one trailing backslash. repr displays that value as 'C:\\logs\\', doubling each backslash so it is visible:

Run this example:

folder = "C:\\logs\\"
raw_folder = r"C:\logs" + "\\"
print(repr(folder))
print(repr(raw_folder))
'C:\\logs\\'
'C:\\logs\\'

repr makes the backslashes visible in the displayed value. The first assignment uses doubled backslashes in the source for each backslash in the value. The second keeps the directory portion readable as a raw string and adds the final backslash with an ordinary string literal. For paths, prefer path-handling libraries when you need to combine or manipulate path components; these examples focus on how Python parses string literals.

Check quotes and verify the fix

If the backslash count looks correct, check that the opening and closing quotes match. For instance, message = "That string ends here' starts with a double quote but has no matching double quote. Python cannot interpret the apostrophe as its closing delimiter. Correct the source to message = "That string ends here" or choose matching single quotes around the whole string.

Use this short check when the diagnostic persists:

  1. Find the reported line and identify the opening quote.
  2. Confirm that a matching quote closes the string before the line ends.
  3. Count consecutive backslashes immediately before that quote; an odd count escapes it.
  4. For a value ending in one backslash, use an ordinary string with an escaped backslash or append one to a raw-string prefix.
  5. Run the corrected source again and read the new diagnostic location if another syntax error remains.

The quoted phrase can help locate older discussions and examples, but trust the behavior and wording of the Python version you are using. Once the string parses, inspect its value with repr if the number or placement of backslashes is unclear.