How to print a single backslash? How to print a single backslash? python python

How to print a single backslash?


You need to escape your backslash by preceding it with, yes, another backslash:

print("\\")

And for versions prior to Python 3:

print "\\"

The \ character is called an escape character, which interprets the character following it differently. For example, n by itself is simply a letter, but when you precede it with a backslash, it becomes \n, which is the newline character.

As you can probably guess, \ also needs to be escaped so it doesn't function like an escape character. You have to... escape the escape, essentially.

See the Python 3 documentation for string literals.


A backslash needs to be escaped with another backslash.

print('\\')


You should escape it with another backslash \:

print('\\')