In this tutorial, I will explain how to fix unterminated string literals in Python and how to resolve them. As a Python developer in the USA, I recently encountered this issue in one of my projects for New York clients. I explored more about this topic and I will share my findings in this article with detailed examples.
Unterminated String Literal in Python
An unterminated string literal error occurs in Python when a string is not properly closed or terminated with a quotation mark. This typically happens when you forget to add the closing quotation mark at the end of the string. Unterminated string literals can manifest in single quotes ('), double quotes ("), or triple quotes (''' or """).
For example, the following would raise a SyntaxError:
company_name = 'Google
print(company_name)The error message clearly states the issue:
SyntaxError: unterminated string literalI have executed the above example code and added the screenshot below.

Read How to Check if a String is an Integer or Float in Python?
Common Causes of Unterminated String Literals
There are a few common scenarios that can lead to unterminated string literal errors in Python:
1. Missing Closing Quote:
Forgetting to close a string with the matching quote results in an error.
Example:
pythonCopyEditdialogue = 'She said, 'Hello!' # Unterminated string literal
Solution: Add the missing closing quote.
dialogue = 'She said, \'Hello!\''Read How to Check if a String is Empty or NaN in Python?
2. Quotes Inside Strings Without Proper Escaping:
Using quotes inside a string without escaping them can break the string.
Example:
pythonCopyEditdialogue = 'She said, 'Hello!' # Unterminated string literal
Solution 1: Escape the inner quotes with a backslash.
pythonCopyEditdialogue = 'She said, \'Hello!\''
Solution 2: Use a different type of quote to define the string.
pythonCopyEditdialogue = "She said, 'Hello!'"
Read How to Split a String into an Array in Python?
3. Multiline Strings:
Forgetting to close multiline strings with triple quotes can lead to errors.
Example:
pythonCopyEditaddress = '''123 Main St,
New York, NY 10001
# Unterminated string literal
Solution: Properly close multiline strings with matching triple quotes.
pythonCopyEditaddress = '''123 Main St,
New York, NY 10001'''
Read How to Split a String and Get the Last Element in Python?
4. Unescaped Backslashes in Strings:
Strings containing backslashes (e.g., file paths on Windows) can cause issues because backslashes are treated as escape characters.
Example:
pythonCopyEditfile_path = 'C:\Users\John\Documents' # Unterminated string literal
Solution 1: Escape backslashes with double backslashes.
pythonCopyEditfile_path = 'C:\\Users\\John\\Documents'
Solution 2: Use raw strings by prefixing the string with r.
pythonCopyEditfile_path = r'C:\Users\John\Documents'
Read How to Split a Sentence into Words in Python?
Conclusion
In this tutorial, I have explained how to fix unterminated string literals in Python and how to resolve them. By understanding the causes and the techniques to properly close and escape strings, you can easily resolve these errors. Remember to double-check your string definitions and use the appropriate quoting style for your needs.
You may also like to read:
- Python Split Regex
- How to Split a String Every N Characters in Python?
- Convert Binary to Decimal in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.