How To Solve SyntaxError: Unterminated Triple-quoted String Literal In Python

SyntaxError: unterminated triple-quoted string literal

When working with strings in Python you may sometimes get the SyntaxError: unterminated triple-quoted string literal. Don’t be panic, we are here to help you with detailed explanations and methods to solve this problem.

Why does the “SyntaxError: unterminated triple-quoted string literal” occur?

There are two types of string syntax: single-quoted and triple-quoted. The single-quoted string starts with a single quote and ends with a single quote, or it can start with a double quote and end with a double quote. Whereas the triple-quoted one starts and ends with a triple of single quotes or double quotes (“) instead. Triple quotes string is multiline docstring; it reads a string that has multiple lines before ending it. For example:

# Single-quoted string:
single = 'LearnShareIT'
double = "LearnShareIT"
 
# Triple-quoted string:
single = '''Learn
Share
IT'''
double = """Learn
Share
IT"""

Both the four strings in the example above represent the same string literal in Python. Now you have learned what the triple-quoted string literal is. The reason you encounter this error is that you are opening a triple-quoted string literal but not closing them so the string literal never terminates. For example:

# Triple-quoted string with opening single quotes only:
single = '''Learn
Share
IT

Or another example:

# Triple-quoted string with opening double quotes:
double = """LearnShareIT

The error also happens when you open a string literal with a triple of single quotes but end with double quotes and vice versa:

# Triple-quoted string with opening double quotes and ending single quotes:
double = """LearnShareIT'''

In the following section, we will provide you with many solutions to solve this error.

How to solve the error?

Close the triple-quoted string literal

As the error indicates, you have the wrong syntax of the triple-quoted string literal, to overcome the problem, you should close them with a triple of quotes (which is the same type as the opening quotes) instead:

# Triple-quoted string using single quotes:
single = '''LearnShareIT'''
print (single)
 
# Triple-quoted string using double quotes:
double = """LearnShareIT"""
print (double)

Output

LearnShareIT
LearnShareIT

Using the single-quoted string literal

Another way to overcome this problem is to use the single-quoted string literal instead of triple-quoted one:

# Single-quoted string using single quotes:
single = 'LearnShareIT'
print (single)

# Single-quoted string using double quotes:
double = "LearnShareIT"
print (double)

Output

LearnShareIT
LearnShareIT

Make sure opening quotes and ending quotes are same type of quotes

We recommend you recheck the quotes at the beginning and ending of the string literals and make sure they are both single quotes or both double quotes, and not to mix them up. For example, if you start the string literals with three single quotes, then you must end it with three single quotes, not double quotes, and vice versa. Also check the number of quotes you have used, because you cannot open a string with three quotes but end with one quote (or two).

Summary

We have learned how to deal with the SyntaxError: unterminated triple-quoted string literal in Python. By following the rules of the string literal syntax in Python as we have explained and finding out the reasons causing this problem in our tutorial, you can easily solve it.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *