1

I want Python interpreter to show me \' as a calculated value. I tried typing "\\'" and it returned me the same thing. If I try "\'" then the backslash is not shown anymore after I hit return. How do I get it to show the backslash like this after I hit return- \' ?

Additional question

This is exact question I am not understanding: Expression -->

'C' + + 'D'

Calculated Value -->

'C\'D'

Find the missing literal

John1024
  • 109,961
  • 14
  • 137
  • 171
mashgreat
  • 66
  • 2
  • 6

2 Answers2

7

Here are two ways to get \':

>>> print("\\'")
\'
>>> print(r"\'")
\'

Discussion

>>> print("\'")
'

The above prints out a single ' because an escaped ' is just a '.

>>> print(r"\'")
\'

The above prints the backslash because r prevents interpretation of escapes.

>>> print("\\'")
\'

The above prints \' because an escaped backslash is a backslash.

Other forms of strings

Using unicode-strings (under python2):

>>> print(u"\\'")
\'
>>> print(ur"\'")
\'

Using byte-codes (also python 2):

>>> print(b"\\'")
\'
>>> print(br"\'")
\'

Answer for additional question

Using the principles described above, the answer for the additional question is:

>>> print('C' + "\\'" 'D')
C\'D
>>> print('C' + r"\'" 'D')
C\'D
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Three ways. There's also `print(u"\\'")`. – Ouroborus Sep 06 '16 at 02:32
  • But it's not. Unicode vs. ASCII. While Python may internally use Unicode, unless you have the particular indicators, the strings you send to it are ASCII. `u` is similar, in this sense, as `r` in that it's telling Python how to interpret the quoted section. – Ouroborus Sep 06 '16 at 02:36
0

print("\'")

repr of a string will never put out a single backslash. repr smartly picks quotes to avoid backslashes. You can get a \' to come out if you do something like '\'"'

Demur Rumed
  • 421
  • 6
  • 17