DEV Community

Cover image for How to fix SyntaxError: unterminated triple-quoted string literal in Python
Reza Lavarian
Reza Lavarian

Posted on • Originally published at decodingweb.dev

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

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)
Enter fullscreen mode Exit fullscreen mode

This post was originally published on decodingweb.dev as part of the Python Syntax Errors series.

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
Enter fullscreen mode Exit fullscreen mode

Adding the missing quotation mark fixes the problem instantly:

# ✅ Correct

message = """Python is a high-level, 
general-purpose 
programming language"""
Enter fullscreen mode Exit fullscreen mode

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""
Enter fullscreen mode Exit fullscreen mode

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

# ✅ Correct

message = """Python is a high-level, 
general-purpose 
programming language"""
Enter fullscreen mode Exit fullscreen mode

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\'''
Enter fullscreen mode Exit fullscreen mode

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\\'''
Enter fullscreen mode Exit fullscreen mode

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\"""
Enter fullscreen mode Exit fullscreen mode

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'''
Enter fullscreen mode Exit fullscreen mode

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

Thanks for reading.

Top comments (0)