-1

Consider the following Python 2.7 code:

 print "\\"

Expected result: \\

Actual result: \

Why does Python only print out a single backslash?

Vingtoft
  • 13,368
  • 23
  • 86
  • 135

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
  • 55,805
  • 14
  • 125
  • 143