Skip to content

How to Fix: Python SyntaxError – EOL while scanning string literal

How to fix Python SyntaxError - EOL while scanning string literal Cover Image

In this tutorial, you’ll learn how to fix one of the most common Python errors: SyntaxError – EOL while scanning string literal. There are three main causes for this error and this tutorial will help you resolve each of these causes. SyntaxErrors are indicative of errors with your code’s syntax. Since your code’s syntax is critically important in how Python interprets your code, knowing how to resolve these types of issues is very important for a Pythonista of any skill level.

By the end of this tutorial, you’ll have learned:

  • What the SyntaxError means
  • What the 3 main causes of the error are and how to resolve them

Resolving a Python SyntaxError: EOL while scanning string literal

To fix the Python SyntaxError: EOL while scanning string literal, you can use any of the following methods:

  1. Close strings that have a missing quotation mark

    If your strings are missing quotation marks at the end, add the matching quotation character.

  2. Use triple quotes for strings that span multiple lines

    If your string spans multiple lines, ensure that you are wrapping your strings in triple quotes.

  3. Escape strings that span multiple lines with the \ character

    If you don’t want to use triple quotes for multi-line strings, be sure to escape each line break with a \ character.

  4. Ensure that strings use matching quotation marks

    If your strings are using mismatching quotation marks, be sure to correct them to matching ones.

Understanding Python SyntaxError: EOL while scanning string literal

What is a SyntaxError in Python?

A Python SyntaxError is raised when the Python interpreter runs into code with syntax that is invalid. For example, if code rules such as closing quotes aren’t followed, Python raises a SyntaxError.

Because syntax errors refer to issues by how your code is written, specifically, fixing these issues relates to fixing the syntax of your code. Thankfully, the traceback that Python returns about the error identifies where the issue can be found.

Specifically, the Python SyntaxError: EOL while scanning string literal refers to strings ending improperly at the end of the line of code. This can point to three roots causes, that we’ll explore in this tutorial!

Now that you have a strong understanding of what this error refers to, let’s identify three of the main causes of this error.

Missing Quotes Causing Python SyntaxError

The most common cause of the Python SyntaxError: EOL while scanning string literal is due to missing quotes at the end of a string. This refers to a string being opened by using either ', ", or """ and not closing the string properly.

Let’s see what this looks like in Python code:

# Raising a SyntaxError When a String Isn't Closed
text = 'Welcome to datagy.io

# Raises:
#   Input In [1]
#     text = 'Welcome to datagy.io
#                                 ^
# SyntaxError: EOL while scanning string literal

The trackback error indicates the line on which the error is happening and where the quote is expected. We can see the arrow points to where the string is expected to be ended.

Beginning in Python 3.10, this error message will become much clearer. In fact, it will raise a different SyntaxError, indicating which string literal hasn’t been closed properly. Let’s see what this looks like in Python 3.10+:

# New SyntaxError in Python 3.10
text = 'Welcome to datagy.io

# Raises
#   File "", line 1
#     text = 'Welcome to datagy.io
#            ^
# SyntaxError: unterminated string literal (detected at line 1)

In order to resolve this issue, we simply need to close the string with a corresponding quote character. Let’s fix this error and run our code without issue:

# Resolving a SyntaxError When Strings Aren't Closed
text = 'Welcome to datagy.io'

In the following section, you’ll learn how to fix a Python SyntaxError caused by strings spanning multiple lines.

Strings Spanning Multiple Lines Causing Python SyntaxError

Another cause of the Python SyntaxError: EOL while scanning string literal error is strings spanning multiple lines. Python allows you to create multi-line strings. However, these strings need to be created with triple quotation marks, using either ''' or """.

The following code will raise a Python SyntaxError:

# Raising a Python SyntaxError When Spanning Multiple Lines
text = 'Welcome to 

datagy.io'

# Raises:
#   Input In [3]
#     text = 'Welcome to
#                        ^
# SyntaxError: EOL while scanning string literal

We can see that the error raised indicates where Python expected a string to be ended. However, in this case it’s not as clear that the string actually spans multiple lines.

We can resolve this type of Python SyntaxError using two methods:

  1. We can wrap our string in triple quotes, or
  2. Escape our line breaks

Resolve Strings Spanning Multiple Lines with Triple Quotes

In order to resolve this error, the string simply needs to be created with a set of triple quotes. This ensures that Python can correctly identify that the string should span multiple lines. Let’s see how we can resolve this error:

# Resolving a Python SyntaxError With Triple Quotes
text = """Welcome to 

datagy.io"""

By using triple-quotes on a string, Python allows your strings to span multiple lines. Similarly, you could use single triple quotes like '''.

Another method to resolve this is to simply escape the line breaks.

Resolve Strings Spanning Multiple Lines with Line Escaping

Python also allows you to escape line breaks by using the \\ character. This lets Python know that the line break is aesthetic and should be ignored. Let’s see how we can resolve our Python SyntaxError using the escape character.

# Resolving a Python SyntaxError With Line Escaping
text = 'Welcome to  \
\
datagy.io'

We can see that this allows the interpreter to properly read the multi-line string. In the following section, you’ll learn how to resolve the third cause of the Python SyntaxError.

Mismatched Quotes Causing Python SyntaxError

The final cause of the Python SyntaxError: EOL while scanning string literal is using mismatched quotation marks. This error occurs when the code uses quotes that aren’t the same style. Because Python allows you to use either ' or ", ensuring that they are used consistently is important.

Let’s see what this error may look like:

# Raising a SyntaxError When Using Mismatched Quotes
text = "Welcome to datagy.io'

# Raises:
#   Input In [6]
#     text = "Welcome to datagy.io'
#                                  ^
# SyntaxError: EOL while scanning string literal

Python indicates that the string isn’t properly closed. It indicates that there should be a supporting quote at the end of the string, following the ' character. We can resolve this error by simply using a matching " character:

# Resolving a Python SyntaxError Caused by Mismatched Quotes
text = "Welcome to datagy.io"

We can see that by using the same type of quote the error is resolved.

Conclusion

In this guide, you learned how to resolve the Python SyntaxError: EOL while scanning string literal. This error has three main causes which were explored throughout. First, you learned how to resolve the error caused by missing quotes. Then, you learned how to resolve the error caused by strings spanning multiple lines. Finally, you learned how to resolve the error caused by mismatched quotes.

Additional Resources

To learn more about related topics, check out the guides below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

2 thoughts on “How to Fix: Python SyntaxError – EOL while scanning string literal”

Leave a Reply

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