How can I print a single backslash?
Question:
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).
Answers:
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.
A hacky way of printing a backslash that doesn’t involve escaping is to pass its character code to chr
:
>>> print(chr(92))
print(fr"{''}")
or how about this
print(r" "[0])
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 – '\'
.
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).
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.
A hacky way of printing a backslash that doesn’t involve escaping is to pass its character code to chr
:
>>> print(chr(92))
print(fr"{''}")
or how about this
print(r" "[0])
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 – '\'
.