简体   繁体   English

在return语句中返回引号

[英]Return the quotation marks in return statement

I made a function that returns a string as shown below but I was wondering how do I keep the double quotes for the quote? 我做了一个返回字符串的函数,如下所示,但我想知道如何保留双引号作为引号?

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)

For example my function returns: 例如我的函数返回:

 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.'

Instead of: (with double quotes) 代替:(带双引号)

'In 1933, a person called Albert Einstein said: "Everything should be made as simple as possible but not simpler."'

You can escape characters (done with \\ ) that have a special meaning to treat them as characters: 您可以转义具有特殊含义的字符(用\\完成),以将其视为字符:

" this is quoted: \\"example\\" "

represents the string: 表示字符串:

this is quoted: "example" 引用:“示例”

I think best way is to use string formating. 我认为最好的方法是使用字符串格式。 And return the variable. 并返回变量。

sentence = 'In {0}, a person called {1} said: "{2}".'.format(year, name, quote)

For complicated string you should use escape characters. 对于复杂的字符串,应使用转义符。 For more details enter link description here 有关更多详细信息,请在此处输入链接描述

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

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