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

Question:

In python (3x version)

'' # this is error no doubt
'\' # this prints two backslashes ,i.e., '\'
r'' # this also gives error
'anytext  anytext' # gives 'anytext \ anytext'

r and R tried both but didn’t work
tried to escape the escape character ,i.e., '\' but didn’t work enter image description here

Asked By: Ritesh Bhartiya

||

Answers:

If I am understanding your questions correctly, like this: print("\")?

Answered By: DUDANF

In IDLE (Python’s Integrated Development and Learning Environment), expression values have their representation echoed to stdout.

As https://docs.python.org/3/library/idle.html explains:

The repr function is used for interactive echo of expression values.
It returns an altered version of the input string in which control
codes, some BMP codepoints, and all non-BMP codepoints are replaced
with escape codes.

If you want to print a string, use the print() function. Otherwise you’ll get its representation.

Consider the following code entered into IDLE:

>>> hitman_str = "Agent  47"
>>> print(hitman_str) # We see only one slash when using print
Agent  47
>>> hitman_str # This shows the representation, which shows two slashes
'Agent \ 47'
>>> print(repr(hitman_str)) # Same as above
'Agent \ 47'

There are multiple ways to get a string with only one slash:

single_slash1 = "\"
>>> print(single_slash1)

>>> single_slash2 = " "[0]
>>> print(single_slash2)

>>> single_slash1 == single_slash2
True

Similarly, there are multiple ways to get a string with two consecutive slashes:

>>> two_slashes1 = "\\"
>>> print(two_slashes1)
\
>>> print(repr(two_slashes1))
'\\'
>>> two_slashes1
'\\'
>>> len(two_slashes1)
2
>>> two_slashes2 = r"\"
>>> print(two_slashes2)
\
>>> print(repr(two_slashes2))
'\\'
>>> two_slashes2
'\\'
>>> len(two_slashes2)
2

We can confirm hitman_str only has one slash:

>>> hitman_str.count(single_slash1)
1

We can iterate through the string and print each character and its Unicode code point.
As expected, this shows only one slash:

>>> for char in hitman_str:
    print(char, ord(char))


A 65
g 103
e 101
n 110
t 116
  32
 92
  32
4 52
7 55

Raw strings are very handy, especially for Windows paths if you don’t wanna use os.path or pathlib:

>>> filename = r"C:UsersLee HongDocumentsNew LettersImpatient 1999-06-14.txt" # Works fine
>>> filename = "C:UsersLee HongDocumentsNew LettersImpatient 1999-06-14.txt" # Error
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape
>>> raw_str = r"This \has 11 \slashes \and no line break"
>>> print(raw_str)
This \has 11 \slashes \and no line break
>>> raw_str.count(single_slash1)
11

For more information, including a list of escape sequences to watch out for, refer to https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

Answered By: GordonAitchJay
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.