Skip to main content
 首页 » 编程设计

Python 字符串文字之包括字符串中的单引号和双引号

2025年05月04日22bhlsheji

我要加入这一系列的字符串:

my_str='"hello!"' + " it's" + ' there' 

想要的结果是:

my_str 
Out[65]: '"hello!" it's there' 

但是我得到:

my_str 
Out[65]: '"hello!" it\'s there' 

我已经尝试了几次迭代,但似乎都不起作用。

请您参考如下方法:

结果正确。单引号必须在单引号字符串中转义。双引号也一样。

如果您尝试打印 结果,您将看到它如您所料。

>>> print(my_str) 
"hello!" it's there