218

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

Does anyone know what I am doing wrong?

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

19 Answers19

285

You are not putting a " before the end of the line.

Use """ if you want to do this:

""" a very long string ...... 
....that can span multiple lines
"""
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
aaronasterling
  • 68,820
  • 20
  • 127
  • 125
123

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.

Community
  • 1
  • 1
Chris H
  • 1,231
  • 1
  • 9
  • 2
  • 4
    @Leo they are called 'escape characters', and this is pretty standard. You can put an `r` before the string to avoid them causing problems. – eric Feb 26 '19 at 16:56
  • @eric you can do that if the \ is not at the very end of the string like `r'the cat in the hat\'`. I wish this wasn't the case, but it is – scrollout Feb 09 '22 at 15:27
  • 1
    @scrollout good point I really wish they would fix that. It almost defeats the purpose. – eric Feb 09 '22 at 21:26
19

(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.....\
...\
...."
JanC
  • 1,264
  • 9
  • 6
13

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\\

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Ashish kulkarni
  • 644
  • 2
  • 7
  • 13
  • I have the same problem but this doesn't solve the issue. Putting additional 2 slashes makes first of them escape for the second one. And the last slash still causes problem. – I.P. Mar 24 '21 at 18:45
  • @I.P. that appears to have been a typo in the post; it should be fixed now. However, see also [Windows path in Python](https://stackoverflow.com/questions/2953834/). – Karl Knechtel Aug 07 '22 at 04:18
12

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'))
  .....
nicbou
  • 1,047
  • 11
  • 16
  • I passed over this answer at first because it focused on the \r\n. The real issue here is `ast.literal_eval` which raises this error whenever it doesn't get a valid python literal. In my case I broke it with something like `ast.literal_eval('{"my": "dict"}"}')`. – Noumenon Jun 14 '22 at 15:43
9

You can try this:

s = r'long\annoying\path'
Dino
  • 7,779
  • 12
  • 46
  • 85
ZakS
  • 1,073
  • 3
  • 15
  • 27
5

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

madhu131313
  • 7,003
  • 7
  • 40
  • 53
4

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.

user12711
  • 693
  • 1
  • 7
  • 21
4

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
Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
4

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

us_david
  • 4,431
  • 35
  • 29
4

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

shoaib21
  • 477
  • 1
  • 6
  • 10
3

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.

Ram Dwivedi
  • 470
  • 3
  • 11
2

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............"""
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Khandelwal-manik
  • 353
  • 3
  • 13
2

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 \
............"
1

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'''
Areeha
  • 823
  • 7
  • 11
1

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.

Md.Rakibuz Sultan
  • 759
  • 1
  • 8
  • 13
0

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............'''

grepit
  • 21,260
  • 6
  • 105
  • 81
0

The error "SyntaxError: EOL while scanning string literal" occurs when there is an issue with the way a string is defined in the code. The error message indicates that the end of the string was reached before the string was closed properly. Note: make sure that the file paths are properly escaped using the backslash character ()

Bangash Owais
  • 61
  • 1
  • 7
0

use \ in the end of window path, and it will work just fine, as shown below

basePath = r'C:\Users\asaini2\OneDrive - Discover\arpan\Principal Data Science\git-project-config\\'
Arpan Saini
  • 4,623
  • 1
  • 42
  • 50