[python] python: SyntaxError: EOL while scanning string literal

I have the above-mentioned error in s1="some very long string............"

Does anyone know what I am doing wrong?

This question is related to python string string-literals

The answer is


In my situation, I had \r\n in my single-quoted dictionary strings. I replaced all instances of \r with \\r and \n with \\n and it fixed my issue, properly returning escaped line breaks in the eval'ed dict.

ast.literal_eval(my_str.replace('\r','\\r').replace('\n','\\n'))
  .....

I also had this exact error message, for me the problem was fixed by adding an " \"

It turns out that my long string, broken into about eight lines with " \" at the very end, was missing a " \" on one line.

Python IDLE didn't specify a line number that this error was on, but it red-highlighted a totally correct variable assignment statement, throwing me off. The actual misshapen string statement (multiple lines long with " \") was adjacent to the statement being highlighted. Maybe this will help someone else.


I faced a similar problem. I had a string which contained path to a folder in Windows e.g. C:\Users\ The problem is that \ is an escape character and so in order to use it in strings you need to add one more \.

Incorrect: C:\Users\

Correct: C:\\\Users\\\


All code below was tested with Python 3.8.3


Simplest -- just use triple quotes.
Either single:

long_string = '''some
very 
long
string
............'''

or double:

long_string = """some
very 
long
string
............"""

Note: triple quoted strings retain indentation, it means that

long_string = """some
    very 
    long
string
............"""

and

long_string = """some
    very 
long
string
............"""

or even just

long_string = """
some
very 
long
string
............"""

are not the same.
There is a textwrap.dedent function in standard library to deal with this, though working with it is out of question's scope.


You can, as well, use \n inside a string, residing on single line:

long_string = "some \nvery \nlong \nstring \n............"

Also, if you don't need any linefeeds (i.e. newlines) in your string, you can use \ inside regular string:

long_string = "some \
very \
long \
string \
............"

In my case, I forgot (' or ") at the end of string. E.g 'ABC' or "ABC"


(Assuming you don't have/want line breaks in your string...)

How long is this string really?

I suspect there is a limit to how long a line read from a file or from the commandline can be, and because the end of the line gets choped off the parser sees something like s1="some very long string.......... (without an ending ") and thus throws a parsing error?

You can split long lines up in multiple lines by escaping linebreaks in your source like this:

s1="some very long string.....\
...\
...."

Most previous answers are correct and my answer is very similar to aaronasterling, you could also do 3 single quotations s1='''some very long string............'''


I too had this problem, though there were answers here I want to an important point to this after / there should not be empty spaces.Be Aware of it


I had this problem - I eventually worked out that the reason was that I'd included \ characters in the string. If you have any of these, "escape" them with \\ and it should work fine.


Your variable(s1) spans multiple lines. In order to do this (i.e you want your string to span multiple lines), you have to use triple quotes(""").

s1="""some very long 
string............"""

In my case with Mac OS X, I had the following statement:

model.export_srcpkg(platform, toolchain, 'mymodel_pkg.zip', 'mymodel.dylib’)

I was getting the error:

  File "<stdin>", line 1
model.export_srcpkg(platform, toolchain, 'mymodel_pkg.zip', 'mymodel.dylib’)
                                                                             ^
SyntaxError: EOL while scanning string literal

After I change to:

model.export_srcpkg(platform, toolchain, "mymodel_pkg.zip", "mymodel.dylib")

It worked...

David


In my case, I use Windows so I have to use double quotes instead of single.

C:\Users\Dr. Printer>python -mtimeit -s"a = 0"
100000000 loops, best of 3: 0.011 usec per loop

You can try this:

s = r'long\annoying\path'

I was getting this error in postgresql function. I had a long SQL which I broke into multiple lines with \ for better readability. However, that was the problem. I removed all and made them in one line to fix the issue. I was using pgadmin III.


In this case, three single quotations or three double quotations both will work! For example:

    """Parameters:
    ...Type something.....
    .....finishing statement"""

OR

    '''Parameters:
    ...Type something.....
    .....finishing statement'''

I had faced the same problem while accessing any hard drive directory. Then I solved it in this way.

 import os
 os.startfile("D:\folder_name\file_name") #running shortcut
 os.startfile("F:") #accessing directory

enter image description here

The picture above shows an error and resolved output.


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to string-literals

String in function parameter How can I get double quotes into a string literal? What is the backslash character (\\)? How to use Macro argument as string literal? What does the symbol \0 mean in a string-literal? Single quotes vs. double quotes in C or C++ python: SyntaxError: EOL while scanning string literal Difference between string object and string literal Windows path in Python C++ multiline string literal