ItsMyCode |
Python is an interpreted language that means, unlike other languages( Java, C++, C#, etc. ), Python doesn’t convert the entire code into low-level machine code at once; rather, each line of code is executed line by line.
The Syntax Error mainly occurs when the Python interpreter finds some anomalies in your code. Some of the common issues are “ missing out the brackets ,” “missing quotes ,” “ if-else mismatch ,” etc.
EOL stands for “ End of line ,” and the Syntax error means that we have not followed the guidelines of Python Programming.
If you read the error carefully, Python is expecting a character at the end of the line. In this instance, Python expects the string closing character ( "
), which needs to be closed at the end of the line.
The eol while scanning string literal error in Python occurs in 3 main use cases.
String in Python must be opened and closed properly with quotations marks; else, you will get a syntax error. Let’s take an example of a string that is not closed.
def getName():
print("My name is Chandler Bing)
getName()
# Output
File "c:\Projects\Tryouts\listindexerror.py", line 2
return "My name is Chandler Bing
^
SyntaxError: EOL while scanning string literal
Solution – We can easily fix this error by closing the string with the same quotation mark we used to open our string in the beginning.
def getName():
print("My name is Chandler Bing")
getName()
# Output
My name is Chandler Bing
In some cases, the string value can span multiple lines if you don’t use a proper syntax in the multi-line string, then Python will throw eol while scanning string literal error.
def getMessage():
message= "This is Chandler Bing and i am one of the famous actor in the
TV Series called Friends. Welcome to My world"
print(message)
getMessage()
# Output
File "c:\Projects\Tryouts\listindexerror.py", line 2
message= "This is Chandler Bing and i am one of the famous actor in the
^
SyntaxError: EOL while scanning string literal
Solution – If you have a multi-line string in Python, the best way to declare it is by enclosing it using triple quotes.
Either enclose it by using 3 single quotation(''' Hello ''')
marks or 3 double quotation (""" Hello """)
marks to resolve the issue.
Alternatively, you can declare the string in one line and use \n
to split wherever required into multi-line.
def getMessage():
message= """This is Chandler Bing and i am one of the famous actor in the
TV Series called Friends. Welcome to My world"""
print(message)
getMessage()
# Output
This is Chandler Bing and i am one of the famous actor in the
TV Series called Friends. Welcome to My world
The type of quote used to open a string should be the same as closing the string that means if you have used single quotes ('
) to open a string, do use single quotes ('
) to close a string.
def getMessage():
message= "Hello World'
print(message)
getMessage()
# Output
File "c:\Projects\Tryouts\listindexerror.py", line 2
message= "Hello World'
^
SyntaxError: EOL while scanning string literal
Solution – Changing the quotation mark("
) to match the beginning of the string will resolve the issue here.
def getMessage():
message= "Hello World"
print(message)
getMessage()
# Output
Hello World
In storing the directory path in a variable, we often use backlash, which will lead to eol while scanning string literal error.
It causes a Syntax error because Python interprets backslash(\)
as an escape sequence. So, in this case, it will treat both (\")
as a single character, and according to Python, the string is not enclosed properly.
# Storing a directory path
folderPath= "C:\Program Files\Python\"
print(folderPath)
# Output
File "c:\Projects\Tryouts\listindexerror.py", line 2
folderPath= "C:\Program Files\Python\"
^
SyntaxError: EOL while scanning string literal
Solution- Replace the backslash with an escape sequence, adding double backslash so that Python can interpret and execute properly.
# Storing a directory path
folderPath= "C:\\Program Files\\Python\\"
print(folderPath)
# Output
C:\Program Files\Python\
The post Python syntaxerror: eol while scanning string literal appeared first on ItsMyCode.