The SyntaxError: EOF while scanning triple-quoted string literal error occurs when you miss the triple-quote in the multiline string. The multiline string is enclosed with a triple quote. If the triple-quote is missed in the multiline string, the EOF (end of file) character is detected. The error SyntaxError: EOF while scanning triple-quoted string literal will be shown if python reaches the end of line character.
The string is enclosed with either double quotes or sing quote in python. Un-like other languages, python supports multiline strings. The multiline strings are enclosed with triple quotes. If there is a issue with creating multiline string, the python compiler will throw this error “SyntaxError: EOF while scanning triple-quoted string literal”.
The multiline string begins with a triple-quote. There may be one or more lines in the string. The multiline will end with another triple-quote. In this article, we will see what this error is about, how to replicate this error, how to resolve this error.
Exception
If there is any mismatch about this triple quote or any problem about creating a multiline string, the error will be thrown. The error will look like the one below.
File "/Users/python/Desktop/test.py", line 9
print("Hello, World!")
^
SyntaxError: EOF while scanning triple-quoted string literal
[Finished in 0.1s with exit code 1]
Root Cause
A multiline string is a string that is created in multiple lines. There is one or more new line characters in the string. The multiline string includes one or more lines. Detailed information is stored in multiple lines. The search and manipulation of these multiline strings is complex. Because one or more new line characters are available in a multiline string.
The multiline string is enclosed with a triple quote. (“”). If the multiline string does not start or end with the triple-quote, the python compiler may throw this exception. The python compiler identifies the beginning of the multiline string and does not see the end of the multiline string. For most cases, this error will be displayed at the end of the python file or at the beginning of the next multiline string.
How to reproduce this issue
Create a multiline string in the python program by enclosing a triple-quote. Delete the triple quote at the end of the multiline list. The python compiler should look at the end of the multiline list. As the python compiler does not see the end triple-quote inside the program, this error will be thrown.
The code below illustrates how to create this exception.
Example 1
print("Hello, World!")
"""
This is a
multiline
comment
print("Hello, World!")
Output
File "/Users/python/Desktop/test.py", line 10
^
SyntaxError: EOF while scanning triple-quoted string literal
[Finished in 0.0s with exit code 1]
Example 2
print("Hello, World!")
"""
This is a
multiline
comment
print("Hello, World!")
"""
The second mult line
comment string
added here
"""
print("Hello, World!")
Output
File "/Users/python/Desktop/test.py", line 10
The second mult line
^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]
Solution 1
Identify a multiline string in the python program. Ensure sure that the multiline string is enclosed with the triple quote. The error is due to the absence of a triple quote either at the beginning or at the end of the multiline. Apply the triple quote accordingly.
In most instances, this error is seen at the end of the python code. Check for the triple quote in the file and fix the multiline string problem.
test.py
print("Hello, World!")
"""
This is a
multiline
comment
"""
print("Hello, World!")
Output
Hello, World!
Hello, World!
[Finished in 0.1s]
Solution 2
If the program includes a triple quote without a multiline string, delete the triple quote. If no variable is assigned to a multiline string, the multiline string is not used. You may delete the multiline string.
The multiline string is used to add a comment to the program. Make sure it is not used for comments.
test.py
print("Hello, World!")
print("Hello, World!")
Output
Hello, World!
Hello, World!
[Finished in 0.1s]
Solution 3
Check the triple quotes in the multiline series. Just two quotations are provided by mistake. In this case, add the third quotation to the multiline string. This is going to fix the problem.