How to fix SyntaxError: unterminated triple-quoted string literal in Python

Updated Sep 22, 2023 ⤳ 4 min read

Python raises “SyntaxError: unterminated triple-quoted string literal” when you use a pair of triple quotes (""" or ''') around a multi-line string literal, but the ending part is missing.

Here’s what the error looks like on Python version 3.11:


File /dwd/sandbox/test.py, line 1
  message = '''Readme:
            ^
SyntaxError: unterminated triple-quoted string literal (detected at line 3)

On the other hand, the error "SyntaxError: unterminated triple-quoted string literal" means Python was expecting a closing triple-quote, but it didn't encounter any:


# 🚫 SyntaxError: unterminated triple-quoted string literal (detected at line 5)

message = """Python is a high-level, 
general-purpose 
programming language

Adding the missing quotation mark fixes the problem instantly:


# ✅ Correct

message = """Python is a high-level, 
general-purpose 
programming language"""

🎧 Debugging Jam

Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀

How to fix "SyntaxError: unterminated triple-quoted string literal"

The error "SyntaxError: unterminated triple-quoted string literal" occurs under various scenarios:

  1. Missing the closing triple quotes
  2. When a string value ends with a backslash (\)
  3. Opening and closing triple quotes mismatch

1. Missing the closing triple quotes: As mentioned earlier, the most common reason behind this error is to forget to close your "triple-quoted string literal" with triple quotes (""") - perhaps you put a double-quote ("") instead of three:


# 🚫 SyntaxError: unterminated triple-quoted string literal (detected at line 5)

message = """Python is a high-level, 
general-purpose 
programming language""

Needless to say, adding the missing quotation mark fixes the problem:


# ✅ Correct

message = """Python is a high-level, 
general-purpose 
programming language"""
You might like:

2. When a string value ends with a backslash (\): Based on Python semantics, triple quotes work as a boundary for multi-line string literals.

However, if your string literal ends with a backslash, the backslash (as the escaping character) will neutralize one of the three quotation marks - making our magical triple-quote lose its quoting behavior.

In programming terminology, we call it an escape character.

Imagine you need to define a string that ends with a \, like a file path on Windows because Windows uses a backslash as the path separator.

But this path separator happens to be the escaping character in most programming languages, including Python:


# 🚫 SyntaxError: unterminated triple-quoted string literal (detected at line 3)

message = '''Readme:
The file is located under the following path:
c:\files\'''

In the above code, the last \ escapes the first (of the three) quotation marks, leaving our string unterminated. As a result, Python raises "SyntaxError: unterminated triple-quoted string literal".

To fix it, we use a double backslash \\ instead of one. The first \ escapes the escaping behavior of its following backslash, and as a result, the \ would be another ordinary character in the string.


# ✅ Escaping a slash in a triple-quoted string literal

message = '''Readme:
The file is located under the following path:
c:\files\\'''
You might like:

3. Opening and closing triple quotes mismatch: The opening and closing triple-quotes must be identical, meaning if the opening part is ''', the closing part must be ''' too.

The following code raises the error since we open it with ''' but close it with """.


# 🚫 SyntaxError: unterminated triple-quoted string literal (detected at line 3)

message = '''Readme:
The file is located under the following path:
c:\files\"""

No matter which one you choose, they need to be identical:


# ✅ Opening and closing quotation marks match

message = '''Python is a high-level, 
general-purpose 
programming language'''
You might like:

Alright, I think it does it. I hope this quick guide helped you solve your problem.

Thanks for reading.

Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.

`
Exit mobile version