October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Fix Unterminated String Literals in Python

Find the quote, newline, or trailing backslash preventing Python from recognizing the end of a string, then apply the correction that preserves the intended value.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To fix Python’s SyntaxError: unterminated string literal, find where the string starts, then make sure it ends with the matching quote—or correct the quote, newline, or trailing backslash that prevents Python from finding the end. Check the reported line and the one before it: the typo may begin before the error marker.

What Python means by “unterminated string literal”

A string literal begins with a quote and must end with the same kind of quote. Python’s language reference puts it plainly: “The quote used to start the literal also terminates it.” A missing closing quote, a quote inside the text, or a line break or backslash that changes how Python reads the delimiter can leave the string unfinished. Python 3.14.8 Language Reference: String and Bytes literals.

Fix the cause that matches your code

Missing closing quote

Add the matching quote where the text is meant to end. A single quote must close with a single quote; a double quote must close with a double quote.

# Broken
message = "Hello

# Fixed
message = "Hello"

Also inspect the previous line. A backslash at its end can escape the newline, making Python treat the next line as part of the same string.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A matching quote inside the text

If the text contains the same quote used to open the string, Python may treat that character as the end. Switch to the other quote style or escape the embedded quote.

message = "Don't do that!"
message = 'Don't do that!'

The same principle applies to a double quote inside a double-quoted string: escape it or enclose the text in single quotes.

An unintended physical newline

An ordinary single- or double-quoted string cannot continue over an unescaped physical newline. If the newline belongs in the value, use three matching quotes at each end:

message = """First line
Second line"""

If the newline is accidental, close the string on the intended line instead. Triple quotes preserve unescaped newlines, so using them changes the value rather than merely hiding the error. Three matching, unescaped quotes close a triple-quoted string. Python 3.14.8 Language Reference: Triple-quoted strings.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A raw string ending in a backslash

The r prefix changes how backslashes are interpreted, but it does not remove the closing-quote requirement. A raw string cannot end with an odd number of backslashes because the final backslash escapes the apparent closing quote. For a Windows-style path, double the final backslash or add it as a separate string:

# Invalid: the final backslash escapes the apparent closing quote
# path = r"C:Temp"

# Valid alternatives
path = "C:\Temp\"
path = r"C:Temp" + "\"

Python 3.12.15 Language Reference: String and Bytes literals.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

If the error is still there

Python may report the error at or after the point where it can no longer parse the code, so the caret is not guaranteed to identify the original typo. Review the reported line and nearby lines for these specific problems:

  • A missing or mismatched closing quote.
  • An unescaped quote inside the string.
  • An accidental physical newline in an ordinary string.
  • An odd number of backslashes at the end of a raw string.
  • A trailing backslash on the preceding line that escapes its newline.

After correcting the delimiter, rerun the same script or a minimal snippet containing the affected line to confirm that the syntax error is gone.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.