-1

Consider the following Python 2.7 code:

 print "\\"

Expected result: \\

Actual result: \

Why does Python only print out a single backslash?

Vingtoft
  • 11,443
  • 17
  • 68
  • 114

1 Answers1

9

It's because \ is the escape character, it escape sequences like newlines and carriage returns. To print out two you can do:

print "\\\\"

Or:

print r"\\"

r prefix tells to ignore escape characters.

Andrew Li
  • 51,385
  • 12
  • 117
  • 139