Syntaxerror eof while scanning triple-quoted string literal

The syntaxerror eof while scanning triple-quoted string literal happens when you are running your code in Python.

But how does this error appear? What are its root causes, and how to fix it?

If you want to know the answers to those questions, then you should keep on reading.

This article will explore the eof while scanning triple-quoted string literal, a Syntax Error in Python.

So without further a do. Let’s strengthen your programming skills.

What is “syntaxerror: eof while scanning triple-quoted string literal”?

The syntaxerror eof while scanning triple-quoted string literal error message occurs when you forgot to place the triple-quote in the multiline string.

The multiline string should be enclosed with a triple quote. If you forgot to place the triple-quote in the multiline string, the EOF (end of file) character is detected.

For example:

sample = """This is a sample from Itsourcecode

As a result, the Syntax Error appears that indicates EOF while scanning triple quoted string literal because when the Python interpreter encounters an unclosed triple-quoted string.

Output:

File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 1
    sample = """This is a sample from Itsourcecode
             ^
SyntaxError: unterminated triple-quoted string literal (detected at line 1)

Are you confused why the output is different, don’t worry because syntaxerror: unterminated triple quoted string literal and syntaxerror: eof while scanning triple quoted string literal are the same.

If you ask with regards to the solution well, both errors have the same solutions as well. You will see it below.

So, in a simple understanding, this error message indicates that a string that was started with three quotation marks (“”” or ”’) was not properly closed with another set of three quotation marks.

What is triple quotes?

The triple quotes (“”” or ”’) in Python are used to define a string that spans multiple lines

These triple quotes are useful in some cases.

For instance, If ever you wanted to print some characters like (“) or (‘).

print """I have found the letter 'I' in the word "Itsourcecode"."""

If you start with “””, you should enclose it with “””, to avoid errors.

Triple quotes have a certain feature. It allows users to define long strings on several lines.

print """Itsourcecode
provides free sourcecodes
and tutorials."""

In Python, when you use triple quotes, you can include line breaks, tabs, and other whitespace characters in the string without using escape characters.

Why does the “eof while scanning triple-quoted string literal” SyntaxError occur?

The eof while scanning triple-quoted string literalSyntaxError occurs when there is an issue with a triple-quoted string in your Python. 

This error technically indicates that Python has reached the end of the file (eof) while scanning a triple-quoted string literal.

There are several reasons why this error may occur in your code:

❌ The triple-quoted string is not properly closed. Python expects triple-quoted strings to have both opening and closing quotes.

❌ When the triple-quoted string is not properly indented, it can also lead to the “eof while scanning triple-quoted string literal” error.

❌ Inconsistent quoting within the string itself. If the opening and closing quotes within the triple-quoted string do not match, Python will raise the SyntaxError.

❌ There are times when this error can be a result of other syntax errors in your code.

❌ Hidden characters or incorrect encodings within the triple-quoted string can also trigger this error.

How to fix the “syntaxerror eof while scanning triple-quoted string literal”?

To fix the SyntaxError: EOF while scanning triple-quoted string literal error, ensure that any string that was started with three quotation marks (“”” or ”’) is properly closed with another set of three quotation marks before the end of the file.

Solution1: Close the triple-quoted string

Ensure that any string that was started with three quotation marks (“”” or ”’) is properly closed with another set of three quotation marks before the end of the file.

Incorrect code:

sample = """This is a sample from Itsourcecode

Corrected code:

sample = """This is a sample from Itsourcecode"""
print(sample)

Output:

This is a sample from Itsourcecode

The corrected code, the triple-quoted string, uses double quotes. Alternatively, you can also use single or double quotes.

For example:

sample = "This is a sample from Itsourcecode"
print(sample)

or

sample = 'This is a sample from Itsourcecode'
print(sample)

The sample string is now enclosed using single or double quotes, so the syntax error is resolved.

Moreover, you have to ensure that you are using exactly 3 quotes to terminate the string.

For example:

sample = """This is a sample from Itsourcecode""
print(sample)

Output:

  File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 1
    sample = """This is a sample from Itsourcecode""
             ^
SyntaxError: unterminated triple-quoted string literal (detected at line 2)

So, you have to check always if you are using 3 quotes, okay 😉

Solution 2: Use an escape character to include line breaks

When you have to include line breaks in your string but you don’t want to use triple quotes, you can use the \n escape character to represent a line break.

For example:

SampleA = """Welcome to Itsourcecode!
SampleB = "We offers free sourcecode and tutorials"

Output:

  File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 1
    SampleA = """Welcome to Itsourcecode!
              ^
SyntaxError: unterminated triple-quoted string literal (detected at line 3)

Corrected code:

SampleA = "Welcome to Itsourcecode!\n"
SampleB = "We offers free sourcecode and tutorials"
result = SampleA + SampleB
print(result)

Output:

Welcome to Itsourcecode!
We offers free sourcecode and tutorials

Conclusion

In conclusion, the syntaxerror eof while scanning triple-quoted string literal error message occurs when you forgot to place the triple-quote in the multiline string.

The multiline string should be enclosed with a triple quote. If you forgot to place the triple-quote in the multiline string, the EOF (end of file) character is detected.

To fix this syntax error: eof while scanning triple-quoted string literal ensure that any string that was started with three quotation marks (“”” or ”’) is properly closed with another set of three quotation marks before the end of the file.

This article already discussed what this error is all about and multiple ways to resolve this error.

By executing the solutions above, you can master this SyntaxError with the help of this guide.

You could also check out other SyntaxError articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Leave a Comment