Syntaxerror: unterminated string literal in Python

What is “syntaxerror unterminated string literal”?

The syntaxerror: unterminated string literal occurs when there is an unterminated string literal somewhere in your code.

In simple terms, Python throws a “SyntaxError: unterminated string literal” error if you forget to add the closing quote in a string.

It’s important to remember that string literals should be enclosed in either single (‘) or double (“) quotes.

Here’s an example of code that would cause a SyntaxError, specifically an unterminated string literal, in both Python and JavaScript

Python:

sample_string = "Hi, welcome to Itsourcecode, this a sample of an unterminated string

JavaScript:

let sampleString = "Hi, welcome to Itsourcecode, this a sample of an unterminated string;

As you have noticed in both examples, the string is not properly enclosed by quotes. The closing quote is missing, as a result, it will throw a SyntaxError.

Why does “unterminated string literal” SyntaxError occur?

Here are the common causes of why this error keeps on bothering you:

👉 One common reason for this error is when the opening and closing quotation marks do not match.

👉 Forgetting to close the string with the correct quotation mark on any line can also trigger this error.

👉 Having special characters within a string, like unescaped quotation marks or newline characters, can interfere with properly ending a string literal.

How to fix the “syntaxerror: unterminated string literal” in Python?

To fix the syntaxerror: unterminated string literal, check if you have to open and close quotes for your string literal, ensure that you have properly escaped your string literal correctly, and avoid splitting your string literal across multiple lines.

Here are the following solutions that can help you to resolve the error.

Solution 1: Add the missing quotation mark

Adding the missing quotation mark fixes the error right away.

For example in Python:

sample_string = "Hi, welcome to Itsourcecode, this a sample of an unterminated string"
print(sample_string)

Output:

Hi, welcome to Itsourcecode, this a sample of an unterminated string

For example in JavaScript:

let sampleString = "Hi, welcome to Itsourcecode, this a sample of an unterminated string";

console.log(sampleString);

Output:

Hi, welcome to Itsourcecode, this a sample of an unterminated string

Solution 2: Split the string across multiple lines using the + operator

If you want to split a string across multiple lines, you can use the + operator to concatenate multiple strings.

For example in Python:

sample_string = ("Hi " ✅
             "welcome to Itsourcecode"
             " this a sample of an unterminated string")
print(sample_string)

Output:

Hi welcome to Itsourcecode this a sample of an unterminated string

For example in JavaScript:

let sampleString = ("Hi, " ✅
                +"welcome to Itsourcecode " 
                + "this a sample of an unterminated string");
console.log(sampleString);

Output:

Hi, welcome to Itsourcecode this a sample of an unterminated string

Solution 3: Use backslash characters or escape quotes within the string

If you want to include a quote character within a string, you need to escape it using a backslash (\).

For example in Python:

sample_string = ("Hi welcome to \"Itsourcecode\" this a sample of an unterminated string")✅
print(sample_string)

Output:

Hi welcome to "Itsourcecode" this a sample of an unterminated string

For example in JavaScript:

let sampleString = ("Hi welcome to \"Itsourcecode\" this a sample of an unterminated string")✅
console.log(sampleString);

Output:

Hi welcome to "Itsourcecode" this a sample of an unterminated string

Solution 4: Use the correct type of quotes

Ensure that you use the correct quotes for your string literals. If you start a string with a single quote (‘), you need to end it with a single quote.

If you start a string with a double quote (“), you need to end it with a double quote.

For example in Python:

sample_string = ('Hi welcome to "Itsourcecode" this a sample of an unterminated string')✅
print(sample_string)

For example in JavaScript:

let sampleString = ('Hi welcome to "Itsourcecode" this a sample of an unterminated string')✅
console.log(sampleString);

Solution 5: Split the string across multiple lines using template literals (JavaScript only)

You can use template literals in JavaScript to split a string across multiple lines.

For example:

let sampleString = `Hi ✅
welcome to Itsourcecode 
this a sample of an unterminated string`;
console.log(sampleString);

Output:

Hi 
welcome to Itsourcecode 
this a sample of an unterminated string

Conclusion

Python throws a “SyntaxError: unterminated string literal” error message if you forget to add the closing quote in a string.

To fix this error, you have to check if you have to open and close quotes for your string literal, ensure that you have properly escaped your string literal correctly, and avoid splitting your string literal across multiple lines.

This article already discussed what this error is all about and multiple ways to resolve this error.

By executing the solutions above, you can master this SyntaxError with the help of this guide.

You could also check out other SyntaxError articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Frequently Asked Questions

What is Python SyntaxError and what causes it?

SyntaxError is raised when Python’s parser can’t understand your code. Common causes: missing colon (def foo() instead of def foo():), unmatched parentheses or brackets, incorrect indentation, mixing tabs and spaces, missing comma in a list, or using a Python 3 feature in a Python 2 interpreter (or vice versa). The error points to a line, but the actual issue is often on the line BEFORE.

How do I fix ‘unexpected EOF while parsing’?

Python ran out of code while expecting more, usually unclosed parenthesis, bracket, brace, or quote. Scroll up from the error line and count opening vs closing pairs. Use an IDE with bracket matching (VS Code, PyCharm) to highlight pairs. Common gotcha: a triple-quoted string that’s missing its closing triple-quote.

Why does my syntax error point to a line that looks correct?

The actual error is often on the line BEFORE the one Python reports. Python reads code until something doesn’t parse, then reports the position where it gave up, not where the user’s mistake started. Check the line above the reported error for missing colons, commas, or closing brackets.

Why does ‘print x’ raise SyntaxError in Python 3?

Python 3 made print a function, so print(x) is required. The bare ‘print x’ syntax was Python 2 only. If you’re following an old tutorial, check whether it targets Python 2 (released 2008) or Python 3 (your current interpreter). Update all print statements to print(…) function calls.

Where can I find more SyntaxError fixes?

Browse the SyntaxError reference hub for 48+ specific fixes (Python and JavaScript). For Python fundamentals see the Python Tutorial hub. For JavaScript syntax errors see the JavaScript Tutorial hub.

Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment