在字符串的开头和结尾添加“”“?

时间:2011-08-21 15:58:21

标签: python string formatting

每当我提供像"Muhammed went to the store and said "Can I have some pickles please""这样的字符串时,由于Can I have some pickles please变得无效,似乎搞砸了字符串的处理方式。所以我认为在它的两边添加“”“肯定会修复它,如果有更好的解决方案,我愿意接受它。

3 个答案:

答案 0 :(得分:4)

你可以逃避内部引号:

"Muhammed went to the store and said \"Can I have some pickles please\""

或使用单引号

'Muhammed went to the store and said "Can I have some pickles please"'

答案 1 :(得分:4)

选择一个:

'Muhammed went to the store and said "Can I have some pickles please"'
"Muhammed went to the store and said \"Can I have some pickles please\""

你不能像这样修理它

"""Muhammed went to the store and said "Can I have some pickles please""""

因为那样你最后会将Muhammed went to the store and said "Can I have some pickles please字符串加上"

一般情况下,您也可以使用

'''Muhammed went to the store and said "Can I have some pickles please"'''

相反,但这对你的例子毫无意义。

答案 2 :(得分:1)

听起来你正在将输入数据放在源代码中。不要那样做;使用文件或sys.stdin代替。