2017-06-06 45 views
0

我有一些硬编码的文本,其中包含许多单引号和双引号。使用除单引号或双引号以外的内容来设置变量?

text = "get in the "van" I said get in the 'van'" 

我可以用\来转义每个双引号,或者我可以切换到外部的单引号并且转义每个单引号。

我想要做的就是能够挑选别的东西,比如! 有什么简单的我可以做吗?在Perl中,我总是能够:

text = qq!this will use "whatever" I'm happy with! 

我正在使用Python 3.6,如果有帮助。

+4

你可以使用三引号:'文本= “”“这将使用“无论”我很高兴!“”“” – khelwood

+0

完美,谢谢!我知道必须有一些东西! – sniperd

回答

4

另一种选择初始化字符串是"""'''

text = """get in the "van" I said get in the 'van'"""

+0

非常好,那正是我需要的。我知道它必须是简单的!我会尽快接受它。 – sniperd

+2

请注意,[PEP008](https://www.python.org/dev/peps/pep-0008/#string-quotes)指出:*“对于三引号字符串,请始终使用双引号字符与PEP 257中的文档字符串约定“* – SiHa

0

您也可以连接多个串

text = 'get in the "van" I said get in the ' "'van'"