117

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 third. What should I do to print a backslash?


This question is about producing a string that has a single backslash in it. This is particularly tricky because it cannot be done with raw strings. For the related question about why such a string is represented with two backslashes, see Why do backslashes appear twice?. For including literal backslashes in other strings, see using backslash in python (not to escape).

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Michael
  • 15,386
  • 36
  • 94
  • 143
  • See also: [python: SyntaxError: EOL while scanning string literal](https://stackoverflow.com/questions/3561691/) for the related error message, and other common causes. – Karl Knechtel Aug 07 '22 at 04:24

4 Answers4

127

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

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 documentation for string literals.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Bucket
  • 7,415
  • 9
  • 35
  • 45
15

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
  • 137,073
  • 23
  • 153
  • 219
  • 1
    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
  • 3
    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
  • 1
    @DarshanP. Please see https://stackoverflow.com/questions/24085680 for details. – Karl Knechtel Aug 04 '22 at 20:58
1
print(fr"\{''}")

or how about this

print(r"\ "[0])
Tae Soo Kim
  • 1,019
  • 8
  • 14
  • 5
    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
  • 1
    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
  • Just do `"\\".join(fp_split)`. How is `fr"\{''}".join(fp_split)` better? – Boris Verkhovskiy Aug 04 '22 at 04:13
1

For completeness: A backslash can also be escaped as a hex sequence: "\x5c"; or a short Unicode sequence: "\u005c"; or a long Unicode sequence: "\U0000005c". All of these will produce a string with a single backslash, which Python will happily report back to you in its canonical representation - '\\'.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153