Python error - or my stupidity - EOL while scanning a string literal

I do not see a significant difference between the next two lines.

But the first showdown, and the second, no.

In [5]: n=""" \\"Axis of Awesome\\" """ In [6]: n="""\\"Axis of Awesome\\"""" File "<ipython-input-6-d691e511a27b>", line 1 n="""\\"Axis of Awesome\\"""" ^ SyntaxError: EOL while scanning string literal 

Is this a Python bug / feature / unusual, or am I missing something fundamental?

+6
source share
2 answers

Last four quotation marks in

 """\\"Axis of Awesome\\"""" 

are parsed as """ , i.e. the end of the line followed by " , i.e. start of a new string literal. However, this new literal never ends. A simple example:

 >>> """foo""""bar" 'foobar' >>> """foo""" "bar" 'foobar' 

If you want to avoid this problem, replace """ with r' or release " :

 >>> """\\"Axis of Awesome\\\"""" '\\"Axis of Awesome\\"' >>> r'\"Axis of Awesome\"' '\\"Axis of Awesome\\"' 
+9
source

Your last 4 quotation marks are evaluated as "" & "" instead of what you expect from his rating as " & """ .

0
source

Source: https://habr.com/ru/post/919667/


All Articles