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 😊

Leave a Comment