SyntaxError: eol while scanning string literal

In this post, we will see about SyntaxError eol while scanning string literal.

SyntaxError are raised when python interpreter is translating source code in bytes and it generally means there is something wrong with the syntax. These errors are also easy to figure out and fix as well.

You will get SyntaxError eol while scanning string literal when Python interpreter checks string literal and if it gets end of line without specific characters(' or " or ''') at the end, it will complain about this syntax error.
[toc]

There can be mainly two reasons for this error.

  1. Missing quotes at the end of string
  2. Multiline string not handled properly

Let’s look at each reason with the help of example.

Missing quotes at the end of string

Missing single quote

str1 = 'Hello world
print(str1)

Output:

  File "", line 1
    str1 = 'Hello world
                       ^
SyntaxError: EOL while scanning string literal

Did you notice we forgot to end string with '?
If you put the single quote(') at end of string, it will fix this error.

str1 = 'Hello world'
print(str1)

Output:

Hello world

Missing double quotes

str1 = "Hello world
print(str1)

Output:

File "", line 1
    str1 = "Hello world
                       ^
SyntaxError: EOL while scanning string literal

Did you notice we forgot to end string with "?
If you put the double quote(") at end of string, it will fix this error.

str1 = "Hello world"
print(str1)

Output:

Hello world

Mixing single and double quotes

If you start a String with double quote(") at end it with single quote(') then also you will get this error.

str1 = "Hello world'
print(str1)

Output:

File "", line 1
    str1 = "Hello world'
                        ^
SyntaxError: EOL while scanning string literal

Did you notice we start string with double quotes(") but ended it with single quote(')?
If you put the double quote(") at end of string, it will fix this error.

str1 = "Hello world"
print(str1)

Output:

Hello world

💡 Did you know?

You can use single quote('), double quote(") or three single quotes(''') or three double quotes(""") to represent string literal.
For example:

str1 = 'java2blog'
str2 = "java2blog"
str3 = '''java2blog'''
str4 = """java2blog"""

All above statements are valid declarations for string literal.

Multiline string not handled properly

You can create multiline string in python using three single quotes(''') or three double quotes(""") but if you use it with double quote(") or single quote('), you will get this error.
Let’s understand this the help of example.

str1 = "Hello world
       from java2blog"
print(str1)

Output:

  File "", line 1
    str1 = "Hello world
                       ^
SyntaxError: EOL while scanning string literal

If you replace double quotes(") with three single quotes('''), SyntaxError will be resolved.

str1 ='''Hello world
       from java2blog'''
print(str1)

Output:

Hello world
       from java2blog

Conclusion

This SyntaxError is easy to figure out and can be easily fixed by handling string properly.

That’s all about eol while scanning string literal.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.