SyntaxError: unterminated string literal – Python

If you are learning Python, one of the first errors you may see is:

SyntaxError: unterminated string literal

This error looks scary, but the reason is very simple:

Python found the start of a string but could not find where it ends.

Let’s understand this with easy examples.


❌ Why This Error Happens

In Python, strings must start and end with the same quote:

  • 'single quotes'
  • "double quotes"

If Python cannot find the closing quote, it throws:

👉 SyntaxError: unterminated string literal


🧪 Example 1 – Missing Closing Quote

message = "Hello World

Output

SyntaxError: unterminated string literal

✅ Fix

message = "Hello World"

🧪 Example 2 – Mixed Quotes

text = "Python is fun'

Started with double quote "
Ended with single quote '

Python gets confused → error.

✅ Fix

Use the same quotes:

text = "Python is fun"

or

text = 'Python is fun'

🧪 Example 3 – Multiline String Mistake

info = "This is line one
This is line two"

Normal quotes cannot span multiple lines.

✅ Fix 1 – Use Triple Quotes

info = """This is line one
This is line two"""

✅ Fix 2 – Use newline character

info = "This is line one\nThis is line two"

🧪 Example 4 – Quote Inside String

msg = "It's a nice day"

This works because outer quotes are double quotes.

But this fails:

msg = 'It's a nice day'

✅ Fix

Use escape character:

msg = 'It\'s a nice day'

or switch quotes:

msg = "It's a nice day"

🧠 Simple Rules to Avoid This Error

  1. Always close strings with the same quote you opened.
  2. For multiline text → use triple quotes.
  3. If your text contains quotes → use:
    • opposite quotes, or
    • escape character \.

✅ Quick Checklist

MistakeSolution
Missing quoteAdd closing quote
Mixed quotesUse same quote type
Multiline stringUse """ """
Quote inside textEscape or change quote

🎯 Interview One-Liner

“SyntaxError: unterminated string literal occurs when Python cannot find the closing quote of a string.”


✨ Final Thought

This error is not about logic —
it’s just Python saying:

“Hey, you started a string… but never told me where it ends!”

Fix the quotes → error gone 🚀

Leave a comment