Update :
From Python 3.6 https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep498, 您可以使用F 弦 https://docs.python.org/3/reference/lexical_analysis.html#f-strings
>>> print(f'"{word}"')
"Some Random Word"
原答案:
你可以试试%-格式 https://pyformat.info/
>>> print('"%s"' % word)
"Some Random Word"
OR str.format https://docs.python.org/3.6/library/string.html#format-examples
>>> print('"{}"'.format(word))
"Some Random Word"
或者使用转义引号字符\ https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
>>> print("\"%s\"" % word)
"Some Random Word"
并且,如果双引号不是限制(即单引号即可)
>>> from pprint import pprint, pformat
>>> print(pformat(word))
'Some Random Word'
>>> pprint(word)
'Some Random Word'
或者像其他人已经说过的那样(将其包含在您的声明中)
>>> word = '"Some Random Word"'
>>> print(word)
"Some Random Word"
使用哪个you感觉更好或更少混乱。
而且,如果您需要对多个单词执行此操作,您不妨创建一个函数
def double_quote(word):
return '"%s"' % word
print(double_quote(word), double_quote(word2))
并且(如果您知道自己在做什么并且)如果您担心这些性能,请参阅这个比较 https://stackoverflow.com/a/48465349/2689986.