I know this is a noob question, but new to Python and trying to understand the following:
Why does the backslash escape work without error here:
>>> print "this is \\\ a string"
this is \\ a string
But when I try:
>>> print "\\\"
I get:
SyntaxError: EOL while scanning string literal
The first backslash escapes the second backslash, and the third escapes the double quote, which then fails to terminate the string. You need either print "\\\\"
or print "\\\""
, depending on what output you are actually trying to get. (The first will print \\
and the second will print \"
.)
Your first \
escapes your second \
. Now the third \
waits for escaping another character but instead gets '
and escapes it. That's why it shows this error. This is the same error you will get if you try to do this
>>> print 'abc # Line ended while scanning string
In this case
>>> print "this is \\\ a string"
The third \
gets a space character which is not a special character. Hence third \
does not escape anything and is the part of the string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With