我有一个变量
sentence = "In 1794, shortly after his arrival in Manchester, Dalton was elected a member of the Manchester Literary and Philosophical Society, the "Lit & Phil", and a few weeks later he communicated his first paper on "Extraordinary facts relating to the vision of colours", in which he postulated that shortage in colour perception was caused by discoloration of the liquid medium of the eyeball. In fact, a shortage of colour perception in some people had not even been formally described or officially noticed until Dalton wrote about his own. Since both he and his brother were colour blind, he recognized that this condition must be hereditary."
现在这可能同时具有“”和“”,因为它将关闭变量的值。我想阻止这个。还有其他存储字符串的方法吗?
答案 0 :(得分:10)
使用\
:
"Some text with \"embedded\" quotes"
如果您的文字只包含双引号,则可以单引号而不必转义双引号:
'Some text with "embedded" quotes'
最后但并非最不重要的一点是,您可以三次外部引号并保存自己也必须转义换行符:
"""Some text with "embedded" quotes"""
"""Some text with "embedded" quotes
and a newline too"""
对于您的示例,单引号已经可以解决问题:
sentence = 'In 1794, shortly after his arrival in Manchester, Dalton was elected a member of the Manchester Literary and Philosophical Society, the "Lit & Phil", and a few weeks later he communicated his first paper on "Extraordinary facts relating to the vision of colours", in which he postulated that shortage in colour perception was caused by discoloration of the liquid medium of the eyeball. In fact, a shortage of colour perception in some people had not even been formally described or officially noticed until Dalton wrote about his own. Since both he and his brother were colour blind, he recognized that this condition must be hereditary.'
答案 1 :(得分:4)
a="""triple quoted strings can contain quote like this " without ending the string"""
答案 2 :(得分:1)
您可以使用以下三重引号:
sentence = """ long sentence with all 'kind" of symbols """