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

Python字符串文字-包括字符串中的单引号和双引号,python,Python,我想加入这一系列字符串: 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!

我想加入这一系列字符串:

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

结果是正确的。单引号必须在单引号字符串中转义。双引号也是如此

如果您尝试打印结果,您将看到它与您预期的一样

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

如果您使用
print
命令,您将看到您想要的

>>> my_str='"hello!"' + " it's" + ' there'
>>> my_str
'"hello!" it\'s there' #Count printed characters. You will count 22
>>> print my_str
"hello!" it's there
#Now count characters. 19
>>> len(my_str)
19
#see count of characters.
仅使用“my_str”而不使用任何命令/函数只显示内存。
但是,如果您希望处理字符串u,则将获得不带“\”的“'”。

如果您使用
print
命令,您将看到您想要的

>>> my_str='"hello!"' + " it's" + ' there'
>>> my_str
'"hello!" it\'s there' #Count printed characters. You will count 22
>>> print my_str
"hello!" it's there
#Now count characters. 19
>>> len(my_str)
19
#see count of characters.
仅使用“my_str”而不使用任何命令/函数只显示内存。
但是,如果您想处理字符串u,则将得到不带“\”的“'”..

打印我的字符串

““你好!”它就在那儿”

您还可以使用
my_str.decode('ascii')

它会将字符串打印为:


“你好!”它就在那里

打印我的字符串
会将您的字符串打印为

““你好!”它就在那儿”

您还可以使用
my_str.decode('ascii')

它会将字符串打印为:


“你好!”它就在那里

你想要得到的结果是不可能实现的。通过不转义
python将假定这是字符串的结尾。因此,您需要使用``显式地转义它,以便正确地解析它。实际上,您可以在堆栈溢出语法highlightingTry
print(my_str)
中很好地看到这一点,并告诉我们是否确实存在问题。您想要得到的结果是不可能实现的。通过不转义
python将假定这是字符串的结尾。因此,您需要使用``显式地转义它,以便正确地解析它。实际上,您可以在堆栈溢出语法highlightingTry
print(my_str)
中很好地看到这一点,并告诉我们是否确实存在问题。