How to fix SyntaxError: unterminated triple-quoted string literal in Python
Tags: python, syntaxerror, programming
Published: 2023-02-01 15:05:51

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)
```

> This post was originally published on [decodingweb.dev](https://www.decodingweb.dev/solved-syntaxerror-unterminated-triple-quoted-string-literal-python) as part of the [Python Syntax Errors](https://www.decodingweb.dev/category/python-syntaxerror) 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:


```python
# 🚫 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:


```python
# ✅ Correct

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

## 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:


```python
# 🚫 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:


```python
# ✅ Correct

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

**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:


```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.


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

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

**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 `"""`.


```python
# 🚫 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:


```python
# ✅ Opening and closing quotation marks match

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

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

Thanks for reading.