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.
Table of Contents
There can be mainly two reasons for this error.
- Missing quotes at the end of string
- 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
1 2 3 4 |
str1 = 'Hello world print(str1) |
Output:
1 2 3 4 5 6 |
File "<ipython-input-1-fdc42ef3a5c8>", 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.
1 2 3 4 |
str1 = 'Hello world' print(str1) |
Output:
Missing double quotes
1 2 3 4 |
str1 = "Hello world print(str1) |
Output:
1 2 3 4 5 6 |
File "<ipython-input-5-28cedcc4c24f>", 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.
1 2 3 4 |
str1 = "Hello world" print(str1) |
Output:
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.
1 2 3 4 |
str1 = "Hello world' print(str1) |
Output:
1 2 3 4 5 6 |
File "<ipython-input-5-28cedcc4c24f>", 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.
1 2 3 4 |
str1 = "Hello world" print(str1) |
Output:
💡 Did you know?
You can use single quote(
'
), double quote("
) or three single quotes('''
) or three double quotes("""
) to represent string literal.
For example:
123456 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.
1 2 3 4 5 |
str1 = "Hello world from java2blog" print(str1) |
Output:
1 2 3 4 5 6 |
File "<ipython-input-10-343f0c2377fc>", line 1 str1 = "Hello world ^ SyntaxError: EOL while scanning string literal |
If you replace double quotes("
) with three single quotes('''
), SyntaxError will be resolved.
1 2 3 4 5 |
str1 ='''Hello world from java2blog''' print(str1) |
Output:
1 2 3 4 |
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.