我在网上搜索,但找不到答案。如何在python中添加单引号作为列表元素的一部分?例如,
foo = ['the']很简单。但如果我想要这样的东西呢?
foo = [''the']元素的位置是‘,并附加单引号?
发布于 2014-04-10 05:30:45
使用另一个引号,如下所示:
foo = ["'the"]
foo = ['"the']
foo = ['''"the''']
foo = ["""'the"""]或者使用“\”
发布于 2014-04-10 05:29:17
在Python中有两种表示字符串的方法(,以避免这个问题,):
some_string = '...'          # single quotes
some_string = "..."          # double quotes因此,您可以使用第二个:
foo = ["'the"]您还可以转义'字符:
foo = ['\'the']发布于 2014-04-10 05:25:31
或者在定义包含单引号字符的字符串时使用双引号,或者使用单引号,并使用\-escape内部单引号:
"'the"
'\'the'两样都有用。
(你的问题与列表无关,只有列表中的字符串.)
https://stackoverflow.com/questions/22979251
复制相似问题