简体   繁体   English

反斜杠转义在Python中不起作用

[英]Backslash escaping is not working in Python

I want Python interpreter to show me \\' as a calculated value. 我希望Python解释器向我显示\\'作为计算值。 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- \\' ? 在按下return- \\'后,如何显示反斜杠?

Additional question 附加问题

This is exact question I am not understanding: Expression --> 这是我不明白的确切问题:表达式->

'C' + 'C'+ + 'D' +'D'

Calculated Value --> 计算值->

'C\\'D' '光盘'

Find the missing literal 找到丢失的文字

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. 上面显示了反斜杠,因为r阻止了转义的解释。

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

The above prints \\' because an escaped backslash is a backslash. 上面的内容会打印\\'因为转义的反斜杠是反斜杠。

Other forms of strings 其他形式的琴弦

Using unicode-strings (under python2): 使用unicode-strings(在python2下):

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

Using byte-codes (also python 2): 使用字节码(也是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

print("\\'")

repr of a string will never put out a single backslash. 字符串的repr永远不会放一个反斜杠。 repr smartly picks quotes to avoid backslashes. repr聪明地选择了引号以避免反斜杠。 You can get a \\' to come out if you do something like '\\'"' 如果您执行类似“ \\”“的操作,则可以得到\\'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM