How to fix “SyntaxError: EOL while scanning string literal” in Python

Updated Oct 23, 2023 β€³ 7 min read

Python raises “SyntaxError: EOL while scanning string literal” error when it reaches the end of the line, yet it hasn’t encountered the expected closing quotation mark (' or ") of a string literal – on the other hand the string literal isn’t properly closed.

This syntax error usually occurs owing to a missing quotation mark or an invalid multi-line string. Here’s what the error looks like:


File test.py, line 1
    book_title = 'Head First Python
                                  ^
SyntaxError: EOL while scanning string literal

Please note this is a Python 2.7 error. Python 3 displays a slightly different error for EOL and string literals.

The term "string literal" refers to every string value in your source code, like 'Head First Python', '49.99', 'test', etc.

Python is an interpreted language that executes lines one after another. So every statement is assumed to be on one line. The new line marks the end of a Python statement and the beginning of another.

However, you can write loops and conditional statements that span multiple lines. You can even store multi-line strings literals by using triple quotes ("""). You can also write multi-line statements by placing a \ at the end of each line (except for the last line).

EOL (End of Line) in an error message usually means Python's interpreter was expecting a symbol - like a quotation mark or a bracket - but it couldn't find any, even until the end of the line.

And the error "EOL while scanning string literal" means Python was expecting a closing quotation mark, but it didn't encounter any:


# 🚫 Raises: SyntaxError: EOL while scanning string literal
book_title = 'Head First Python

Adding the missing quotation mark resolves the issue instantly:


# βœ… Correct
book_title = 'Head First Python'

Let's explore common scenarios that lead to this SyntaxError.

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! πŸŽ΅πŸ’»πŸš€

24/7 lofi music radio banner, showing a young man working at his computer on a rainy autmn night with hot drink on the desk.

How to fix it?

Python "SyntaxError: EOL while scanning string literal" error occurs under various scenarios:

  1. Forgetting to close a string literal with a quotation mark
  2. Having a \ right before the closing quotation mark
  3. Open and closing quotation marks don't match
  4. Having a multi-line string value enclosed with " or '
  5. A missing slash!
  6. Multi-line strings need triple quotes, not quadruple!

1. Forgetting to close a string literal with a quotation mark: The most common reason behind this error is to forget to close your string with a quotation mark - whether it's a single, double, or triple quotation mark.


# 🚫 Raises: SyntaxError: EOL while scanning string literal
book_title = 'Head First Python

Needless to say, adding the missing end fixes the problem:


# βœ… Correct
book_title = 'Head First Python'

2. Having a \ right before the closing quotation mark: Based on Python semantics, a pair of quotation marks work as a boundary for a string literal.

Putting a backslash before a quotation mark will turn it into an ordinary character - without any special behavior.

This is helpful when you want to include a quotation mark in your string, but you don't want it to interfer with its surrounding quotation marks:


message = 'I\' m good'

That said, if you use the backslash before the ending quotation mark, it won't be a boundary character anymore.

Imagine you need to define a string that ends with a \ like a file path on Windows


# 🚫 Raises: SyntaxError: EOL while scanning string literal
file_dir = 'C:\files\'

In the above code, the last \ escapes the quotation mark's behavior, making our string open-ended.

As a result, Python complains with this syntax error.

To fix it, we use a double backslash \\ instead of one. As you probably know, the first \ escapes the effect of its following slash, and as a result, we'll have our slash in the string (as an ordinary character), and the quoting behavior of ' remains intact.


# βœ… Escaping a slash in a string literal
file_dir = 'C:\files\\'

3. Open and closing quotation marks don't match: The opening and closing quotation marks must be identical (matching quotation mark), so if the opening is ", the closing must be " too. The following code raises the error:


# 🚫 Raises: SyntaxError: EOL while scanning string literal
book_title = "Python Head First'

No matter which one you choose, they need to be identical:


# βœ… Opening and closing quotation marks match
book_title = 'Python Head First'

4. Having a multi-line string value enclosed with " or ': If you use ' or " to quote a string literal, Python will look for the closing quotation mark on the same line. So if you define a string literal in multiple lines enclosed in single or double quotes, you'll get the syntax error:


# 🚫 Raises: SyntaxError: EOL while scanning string literal

message = 'Python is a high-level, 
general-purpose 
programming language'

In the above Python code, the first line doesn't end with a quotation mark.

If you want a string literal to span across several lines, you should use the triple quotes (""" or ''') instead:


# βœ… The correct way of defining multi-line strings

message = '''Python is a high-level, 
general-purpose 
programming language'''

5. Multi-line strings need triple quotes, not quadruple: As mentioned earlier, to create multi-line strings, we use triple quotes. But what happens if you use quadruple quotes?

The opening would be ok, as the fourth quote would be considered a part of the string. However, the closing part will cause a SyntaxError.

Since Python expects the closing part to be triple quotes, the fourth quotation mark would be considered a separate opening - without an end part.


# 🚫 Raises: SyntaxError: EOL while scanning string literal

message = ''''Python is a high-level, 
general-purpose 
programming language''''

Always make sure you're not using quadruple quotes by mistake.

6. A missing slash: Another way to span Python statements across multiple lines is by marking each line with a backslash. This backslash (\) escapes the hidden newline character (\n) and makes Python keeps parsing the lines until it reaches a newline character.

You can use this technique as an alternative to the triple quotes:


# βœ… The correct way of defining multi-line strings with '\'

message = 'Python is a high-level, \
general-purpose \
programming language'

Now, if you forget the \ in the second line, Python will expect to see the closing quotation mark on that line:


# 🚫 Raises: SyntaxError: EOL while scanning string literal

message = 'Python is a high-level, \
general-purpose 
programming language'

If you're taking this approach, all lines should end with \ except for the last line, which ends with the closing quotation mark.

βœ‹ Please note: Since the \ is supposed to escape the newline character (the hidden last character), no space should follow the \. In that case, the newline character wouldn't be affected, and Python will expect the statement to end on that line.

Alright, I think it does it. I hope this quick guide helped you solve your problem.

Thanks for reading.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`