Borislav Hadzhiev
Mon Apr 25 2022·2 min read
Photo by Strauss Western
The Python "SyntaxError: unterminated triple-quoted string literal" occurs when we open a triple-quoted string and forget to close it. To solve the error, make sure to close the triple-quoted string.
Here is an example of how the error occurs.
# ⛔️ SyntaxError: unterminated triple-quoted string literal (detected at line 4) example = """ hello world
We opened a triple-quoted string but forgot to close it, so the string never ends.
To solve the error, close the triple-quoted string.
example = """ hello world """ # 👈️ close string # # hello # world # print(example)
The triple-quoted string in the example above uses double quotes, but you can also define one using single quotes.
example = ''' hello world ''' # # hello # world # print(example)
Triple-quotes strings are very similar to basic strings that we declare using single or double quotes.
But they also enable us to:
example = ''' It's Alice "hello" ''' # # It's Alice # "hello" # print(example)
The string in the example above uses both single and double quotes and doesn't have to escape anything.
If you only have to use a single quote in the string, it would be easier to just wrap the string using double quotes.
example = "It's Alice" # 👉️ "It's Alice" print(example)
Conversely, if your string contains double quotes, just wrap it in single quotes to not have to escape anything.