SyntaxError: eol while scanning string literal

Previous
Next

In this post, we will see about SyntaxError eol while scanning string literal.

SyntaxError are raised when python interpreter is translating source code in bytes and it generally means there is something wrong with the syntax. These errors are also easy to figure out and fix as well.

You will get SyntaxError eol while scanning string literal when Python interpreter checks string literal and if it gets end of line without specific characters( ' or " or ''') at the end, it will complain about this syntax error.

Table of Contents

There can be mainly two reasons for this error.

  1. Missing quotes at the end of string
  2. Multiline string not handled properly

Let’s look at each reason with the help of example.

Missing quotes at the end of string

Missing single quote

Output:

Did you notice we forgot to end string with '?
If you put the single quote( ') at end of string, it will fix this error.

Output:

Hello world

Missing double quotes

Output:

Did you notice we forgot to end string with "?
If you put the double quote( ") at end of string, it will fix this error.

Output:

Hello world

Mixing single and double quotes

If you start a String with double quote( ") at end it with single quote( ') then also you will get this error.

Output:

Did you notice we start string with double quotes( ") but ended it with single quote( ')?
If you put the double quote( ") at end of string, it will fix this error.

Output:

Hello world

💡 Did you know?

You can use single quote( '), double quote( ") or three single quotes( ''') or three double quotes( """) to represent string literal.
For example:

All above statements are valid declarations for string literal.

Multiline string not handled properly

You can create multiline string in python using three single quotes( ''') or three double quotes( """) but if you use it with double quote( ") or single quote( '), you will get this error.
Let’s understand this the help of example.

Output:

If you replace double quotes( ") with three single quotes( '''), SyntaxError will be resolved.

Output:

Conclusion

This SyntaxError is easy to figure out and can be easily fixed by handling string properly.

That’s all about eol while scanning string literal.

Previous
Next

Add Comment