Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python SyntaxError: EOL while scanning string literal

Tags:

python

I'm using python 3.5.1. When I was trying this

print(r'\t\\\')

I got the error: SyntaxError: EOL while scanning string literal. But this one worked well

print(r'\t\\')

Can anyone please explain this?

like image 226
Yitong Zhong Avatar asked Sep 05 '16 02:09

Yitong Zhong


1 Answers

See the 3.5 docs on String and Bytes literals:

Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, not as a line continuation.

like image 72
elethan Avatar answered Oct 01 '22 23:10

elethan