-2

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

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

2 Answers2

2

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:\Users\Lee Hong\Documents\New Letters\Impatient 1999-06-14.txt" # Works fine
>>> filename = "C:\Users\Lee Hong\Documents\New Letters\Impatient 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

GordonAitchJay
  • 4,640
  • 1
  • 14
  • 16
  • this does not work see it prints `0.60\textbackslash pm0.073` I want `0.60\pm0.073` – Charlie Parker Nov 17 '21 at 17:22
  • If you want to print `0.60\pm0.073`, you can use a raw string and a single backslash (`print(r"0.60\pm0.073")`), or a just plain string and two backslashes (`print("0.60\\pm0.073")`). – GordonAitchJay Nov 18 '21 at 07:26
-1

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

DUDANF
  • 2,618
  • 1
  • 12
  • 42