[Solved] SyntaxError: unterminated string literal in Python
Python raises “SyntaxError: unterminated string literal” when a string value doesn’t have a closing quotation mark.
This syntax error usually occurs owing to a missing quotation mark or an invalid multi-line string. Here’s what the error looks like on Python version 3.11:
File /dwd/sandbox/test.py, line 1
book_title = 'Head First Python
^
SyntaxError: unterminated string literal (detected at line 1)
On the other hand, the error "SyntaxError: unterminated string literal" means Python was expecting a closing quotation mark, but it didn't encounter any:
# π« SyntaxError: unterminated string literal (detected at line 1)
book_title = 'Head First Python
Adding the missing quotation mark fixes the problem instantly:
# β
Correct
book_title = 'Head First Python'
Psssst! Do you want to learn web development in 2023?
π§ 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 string literal"
The error "SyntaxError: unterminated string literal" occurs under various scenarios:
- Missing the closing quotation mark
- When a string value ends with a backslash (\)
- Opening and closing quotation marks mismatch
- A multi-line string enclosed with
"
or'
- A missing backslash!
- Multi-line strings enclosed with quadruple quotes!
1. Missing the closing quotation mark: The most common reason behind this error is to forget to close your string with a quotation mark - whether it's a single, double, or triple quotation mark.
# π« SyntaxError: unterminated string literal (detected at line 1)
book_title = 'Head First Python
Needless to say, adding the missing end fixes the problem:
# β
Correct
book_title = 'Head First Python'
2. When a string value ends with a backslash (\
): Based on Python semantics, a pair of quotation marks work as a boundary for a string literal.
Putting a backslash before a quotation mark will neutralize it and make it an ordinary character. In the programming terminology, it's called an escape character.
This is helpful when you want to include a quotation mark in your string, but you don't want it to interfer with its surrounding quotation marks:
message = 'I\' m good'
That said, if you use the backslash before the ending quotation mark, it won't be a boundary character anymore.
Imagine you need to define a string that ends with a \
like a file path on Windows
# π« SyntaxError: unterminated string literal (detected at line 1)
file_dir = 'C:\files\'
In the above code, the last \
escapes the quotation mark, leaving our string unterminated. As a result, Python raises "SyntaxError: unterminated string literal".
To fix it, we use a double backslash \\
instead of one. It's like using its effect against itself; the first \
escapes the its following backslash. As a result, we'll have our backslash in the string (as an ordinary character), and the quoting effect of '
remains intact.
# β
Escaping a slash in a string literal
file_dir = 'C:\files\'
3. Opening and closing quotation marks mismatch: The opening and closing quotation marks must be identical, meaning if the opening quotation mark is "
, the closing part must be "
too.
The following code raises the error:
# π« SyntaxError: unterminated string literal (detected at line 1)
book_title = "Python Head First'
No matter which one you choose, they need to be identical:
# β
Opening and closing quotation marks match
book_title = 'Python Head First'
4. A multi-line string enclosed with "
or '
: If you use '
or "
to quote a string literal, Python will look for the closing quotation mark on the same line.
Python is an interpreted language that executes lines one after another. So every statement is assumed to be on one line. A new line marks the end of a Python statement and the beginning of another.
So if you define a string literal in multiple lines, you'll get the syntax error:
# π« SyntaxError: unterminated string literal (detected at line 1)
message = 'Python is a high-level,
general-purpose
programming language'
In the above code, the first line doesn't end with a quotation mark.
If you want a string literal to span across several lines, you should use the triple quotes ("""
or '''
) instead:
# β
The correct way of defining multi-line strings
message = '''Python is a high-level,
general-purpose
programming language'''
5. Multi-line strings enclosed with quadruple quotes: As mentioned earlier, to create multi-line strings, we use triple quotes. But what happens if you use quadruple quotes?
The opening part would be ok, as the fourth quote would be considered a part of the string. However, the closing part will cause a SyntaxError.
Since Python expects the closing part to be triple quotes, the fourth quotation mark would be considered a separate opening without a closing part, and it raises the "SyntaxError: unterminated string literal" error.
# π« SyntaxError: unterminated string literal (detected at line 3)
message = ''''Python is a high-level,
general-purpose
programming language''''
Always make sure you're not using quadruple quotes by mistake.
6. A missing slash: Another way to span Python statements across multiple lines is by marking each line with a backslash. This backslash (\
) escapes the hidden newline character (\n
) and makes Python continue parsing statements until it reaches a newline character.
You can use this technique as an alternative to the triple quotes:
# β
The correct way of defining multi-line strings with '\'
message = 'Python is a high-level, \
general-purpose \
programming language'
Now, if you forget the \
in the second line, Python will expect to see the closing quotation mark on that line:
# π« SyntaxError: unterminated string literal (detected at line 2)
message = 'Python is a high-level, \
general-purpose
programming language'
If you're taking this approach, all lines should end with \
, except for the last line, which ends with the closing quotation mark.
β Please note: Since the
\
is supposed to escape the newline character (the hidden last character), no space should follow the\
. If you leave a space after the backslash, the newline character wouldn't be affected, and Python will expect the statement to end on the current line.
Alright, I think it does it. I hope this quick guide helped you solve your problem.
Thanks for reading.
Never miss a guide like this!
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.