我创建了一个返回字符串的函数,如下所示,但我想知道如何保留引号的双引号?
def quote_maker(quote, name, year):
''' (string, string, number)-> None
Returns a sentence displaying in what given year the
given name of a person said the given quote.
'''
return (' In '+ str(year) +', a person called '+ name +' said: '+ quote)
例如我的函数返回:
quote_maker("Everything should be made as simple as possible but not simpler.", "Albert Einstein", 1933)
'In 1933, a person called Albert Einstein said: Everything should be made as simple as possible but not simpler.'
而不是:(带双引号)
'In 1933, a person called Albert Einstein said: "Everything should be made as simple as possible but not simpler."'
答案 0 :(得分:0)
您可以转义具有特殊含义的字符(使用\
完成)以将其视为字符:
" this is quoted: \"example\" "
表示字符串:
引用:“示例”
答案 1 :(得分:0)
我认为最好的方法是使用字符串格式化。并返回变量。
sentence = 'In {0}, a person called {1} said: "{2}".'.format(year, name, quote)
对于复杂的字符串,您应该使用转义字符。有关详细信息enter link description here