SyntaxError: EOL while scanning string literal (Fix)
This error means Python found the start of a string, but never found the end of it.
In simple terms, you opened a string with a quote, but the closing quote is missing or incorrect. Because this is a syntax error, your code will not run until you fix it.
Quick fix
name = "Alice"
print(name)
message = 'Hello world'
print(message)
This error usually happens when a string starts with a quote but does not end with the matching quote on the same line.
What this error means
- Python reached the end of the line before the string was finished.
- A string started with a quote, but Python could not find the closing quote.
- This is a syntax error, so the code will not run at all until it is fixed.
If you are new to strings, see Python strings explained with basics and examples.
Example that causes the error
Here is a simple example with a missing closing quote:
name = "Alice
print(name)
You will usually see an error like this:
File "test.py", line 1
name = "Alice
^
SyntaxError: EOL while scanning string literal
Python expected the string to end with another " on the same line.
Sometimes the error points to the next line instead. That happens because Python still thinks the string is open.
Common causes
Missing closing single quote
message = 'Hello
Fix:
message = 'Hello'
Missing closing double quote
message = "Hello
Fix:
message = "Hello"
Opening and closing quotes do not match
This starts with a single quote and ends with a double quote:
message = 'Hello"
Fix it by using matching quotes:
message = 'Hello'
or:
message = "Hello"
Apostrophe inside a single-quoted string
This is a very common beginner mistake:
text = 'don't do that'
Python thinks the string ends after don.
Fix it by using double quotes:
text = "don't do that"
You can also escape the apostrophe:
text = 'don\'t do that'
Quote inside the string is not escaped
text = "She said "hello""
Python sees the second " as the end of the string.
Fix it by escaping the inner quotes:
text = "She said \"hello\""
Or use single quotes around the whole string:
text = 'She said "hello"'
Breaking a normal string across lines
This does not work:
text = "Hello
world"
Use triple quotes for a multi-line string:
text = """Hello
world"""
Or join separate strings correctly:
text = "Hello " + "world"
If you want to build strings in different ways, see how to format strings in Python.
How to fix it
Use these checks:
- Add the missing closing quote.
- Make sure the opening and closing quotes match.
- Use double quotes if the text contains an apostrophe.
- Escape inner quotes with a backslash when needed.
- Use triple quotes for multi-line strings.
Here are a few corrected examples:
# Missing closing quote fixed
name = "Alice"
# Matching quotes
color = 'blue'
# Apostrophe handled correctly
answer = "I don't know"
# Inner quotes escaped
quote = "He said \"Python is fun\""
# Multi-line string
message = """Line one
Line two"""
Debugging steps
When you see this error, go through these steps:
- Look at the line named in the error message first.
- Check the line before it too, because the real problem may start earlier.
- Count opening and closing quotes.
- Check whether a quote inside the text ends the string by accident.
- If the string spans lines, replace it with triple quotes or join lines correctly.
You can also run your file directly:
python your_script.py
Or check it without running the program:
python -m py_compile your_script.py
If Python reports a broader syntax problem, compare it with SyntaxError: invalid syntax.
Related errors to compare
Some syntax errors look similar but mean different things:
- SyntaxError: invalid syntax is a general syntax error.
SyntaxError: missing colonhappens when punctuation is missing afterif,for,while,def, orclass.- SyntaxError: unexpected EOF while parsing happens when Python reaches the end of the file with unfinished code.
Common mistakes
These are the most common reasons this error appears:
- A missing closing quote at the end of a string
- Mixed quote types, such as opening with a single quote and closing with a double quote
- An apostrophe inside a single-quoted string like
'don't' - A double quote inside a double-quoted string without escaping it
- Trying to write a multi-line string with normal quotes
Understanding how string quotes work will help you avoid this error again. You can also review the Python str() function explained if you want to understand strings better.
FAQ
What does EOL mean in this error?
EOL means end of line. Python reached the end of the line before the string was closed.
Why does the error sometimes point to the next line?
If Python thinks a string is still open, it may continue reading until the next line before reporting the error.
How do I include an apostrophe inside a string?
Use double quotes around the string, or escape the apostrophe with a backslash.
Example:
text = "don't"
or:
text = 'don\'t'
Can I split a string across multiple lines?
Yes. Use triple quotes for a multi-line string, or combine separate strings correctly.
Example:
text = """first line
second line"""