5

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?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Yitong Zhong
  • 59
  • 1
  • 1
  • 3

1 Answers1

6

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.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
elethan
  • 16,408
  • 8
  • 64
  • 87