Skip to main content
 首页 » 编程设计

Python之如何将双引号附加到字符串并存储为新字符串

2025年05月04日19rubylouvre

我正在使用 Python 2.6+ 并想将双引号附加到字符串并将其存储为新的字符串变量。我不想打印它,但稍后会在我的 python 脚本中使用它。

例如:

a = 'apple' 
b = some_function(a) --> b would be equal to '"apple"' 

我该怎么做?到目前为止,我看过的所有解决方案都只在打印字符串时有效。

请您参考如下方法:

python中的漂亮用法

b = '"{}"'.format(a) 

在 python 3.6(或更高版本)中

b = f'"{a}"' 

同样工作!