'\"atg is a codon, isn\'t it?\" \"Yes, it is\", he answered'
作为输出:
'"atg is a codon, isn\'t it?" "Yes, it is", he answered'
为什么转义符出现在输出中?
当我输入下面的字符串时,这不会发生。
'This is a codon, isn\'t it?'
我得到的输出是:
"This is a codon, isn't it?"
答案 0 :(得分:2)
因为在第一个字符串中,整个字符串在一个引号中,因此应该转义另一个单引号。而在第二个中,整个字符串是双引号。
>>> '\"atg is a codon, isn\'t it?\" \"Yes, it is\", he answered'
'"atg is a codon, isn\'t it?" "Yes, it is", he answered'
^ ^
>>>
>>> 'This is a codon, isn\'t it?'
"This is a codon, isn't it?" # there is no need to escape the one-quote between double-quotes
^ ^
答案 1 :(得分:0)
需要转义字符,因为这是一个字符串,如果确切地输入,则会重现字符串。该字符串包含单引号和双引号,因此包含该字符串的任何类型都需要在字符串中进行转义。
答案 2 :(得分:0)
您可以选择单引号或双引号来包含字符串。必须使用'\'转义任何出现的封闭引号字符。
>>> b = 'Hello\'s'
>>> a = "Hello's"
>>> b = 'Hello\'s'
>>> c = 'Hello"s'
>>> d = "Hello\"s"
>>> a
"Hello's"
>>> b
"Hello's"
>>> c
'Hello"s'
>>> d
'Hello"s'
>>>