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.
Missing quotes at the end of string
Missing single quote
Python
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.
Python
1
2
3
4
str1 = 'Hello world'
print(str1)
Output:
Hello world
Missing double quotes
Python
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.
Python
1
2
3
4
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.
Python
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.
Python
1
2
3
4
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:
Python
1
2
3
4
5
6
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.
Python
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.
Python
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.[Source]-https://java2blog.com/syntaxerror-eol-while-scanning-string-literal/
We provide the best Advanced Java training, navi mumbai. We have industry experienced trainers and provide hands on practice. Basic to advanced modules are covered in training sessions.
Comments
Post a Comment