106

When I write print('\') or print("\") or print("'\'"), Python doesn't print the backslash \ symbol. Instead it errors for the first two and prints '' for the second. What should I do to print a backslash?

wjandrea
  • 23,210
  • 7
  • 49
  • 68
Michael
  • 14,053
  • 33
  • 90
  • 141

5 Answers5

121

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.

wjandrea
  • 23,210
  • 7
  • 49
  • 68
Bucket
  • 7,139
  • 9
  • 32
  • 45
14

A backslash needs to be escaped with another backslash.

print('\\')
Boris Verkhovskiy
  • 10,733
  • 7
  • 77
  • 79
Andy
  • 46,308
  • 56
  • 161
  • 219
11

A hacky way of printing a backslash that doesn't involve escaping is to pass its character code to chr:

>>> print(chr(92))
\
Jean-François Fabre
  • 131,796
  • 23
  • 122
  • 195
  • Heyy man!! Nice +1 this was rebellious!! However, In interactive interpreter `s = chr(92)` is `'\\'` but when `print(s)` this gives `\\` – Darshan P. Dec 20 '21 at 06:06
  • 1
    this is expected and well known as the interpreter prints stuff that you can copy/paste into your program (representation) as opposed as printing it (string conversion). See `__str__` versus `__repr__` – Jean-François Fabre Dec 20 '21 at 09:43
  • Thanks for sharing `__str__` vs `__repr__` as I'm new to python. – Darshan P. Dec 20 '21 at 13:17
8

You should escape it with another backslash \:

print('\\')
Diggy.
  • 6,344
  • 3
  • 17
  • 37
lelloman
  • 13,345
  • 5
  • 59
  • 81
0
print(fr"\{''}")

or how about this

print(r"\ "[0])
Tae Soo Kim
  • 659
  • 6
  • 12
  • 4
    If you're deliberately trying to make your code harder to read, you can also do something like `import base64; print(base64.b64decode(b'XA==').decode('ascii'))`. – Boris Verkhovskiy Feb 10 '21 at 08:28
  • Okay can you also tell me how to join off a single backslash? – Tae Soo Kim Feb 10 '21 at 10:26
  • what? do you mean `"\\".join([])`? – Boris Verkhovskiy Feb 10 '21 at 10:27
  • Actually, since paths in python require raw strings so often, it is easy to confuse using fr"\\" and "\\". That is why at one point this was used – Tae Soo Kim Feb 10 '21 at 10:52
  • So you're writing all your Windows paths like `fr"C:\{''}\{''}Program Files\{''}system32\{''}programs"` because you're afraid you'll forget the `r`? The better solution there might be to use [pathlib](https://docs.python.org/3/library/pathlib.html), then you can use `/` slashes in Windows paths `PureWindowsPath('c:/foo/bar/setup.py')`. What's your reasoning for `r"\ "[0]`? – Boris Verkhovskiy Feb 10 '21 at 12:07
  • return [os.path.abspath(fr"\{''}".join(fp_split)) for fp_split in globbed_files_split if fp_split[-1] not in processed_globbed_files] Basically when working with os module, you end up using fr everywhere. Joining can lead to this error, then you forget (or don't know) that raw double backslashes accumulate. – Tae Soo Kim Feb 10 '21 at 14:39
  • Also I do believe that Tensorflow has many issues with forward slashes depending on the OS. I think it even is incorrect in sharded tfrecords, and can causes lots of trouble. Pathlib is unnecessary to use, as os modules are universal to different places aswell (such as nodejs), and os module is more powerful, and quicker to use. Plus do you really want to cast all your paths to strings – Tae Soo Kim Feb 10 '21 at 14:52