使用qq,Perl允许使用几乎任何字符作为引号来定义包含'和"的字符串,而无需转义它们:
qq(She said, "Don't!")
qq¬And he said, "I won't."¬(特别方便,因为我的键盘上有几乎从未使用过的¬ )。
Python有类似的吗?
发布于 2015-04-10 11:11:47
不能将任意字符定义为引号,但如果需要在字符串中同时使用'和",则可以使用多行字符串:
>>> """She said "that's ridiculous" and I agreed."""
'She said "that\'s ridiculous" and I agreed.'但是,请注意,如果您使用的引号类型也是字符串中的最后一个字符,Python会感到困惑:
>>> """He yelled "Whatever's the matter?""""
SyntaxError: EOL while scanning string literal所以在这种情况下你必须转换:
>>> '''He yelled "Whatever's the matter?"'''
'He yelled "Whatever\'s the matter?"'纯粹作为一种选择,您可以将字符串拆分为每个引号类型都有且不具有引用类型的部分,并依赖Python隐式连接连续字符串:
>>> "This hasn't got double quotes " 'but "this has"'
'This hasn\'t got double quotes but "this has"'
>>> "This isn't " 'a """very""" "attractive" approach'
'This isn\'t a """very""" "attractive" approach'发布于 2015-04-10 11:09:40
您可以使用三重单引号或三重双引号。
>>> s = '''She said, "Don't!"'''
>>> print(s)
She said, "Don't!"
>>> s = """'She sai"d, "Don't!'"""
>>> print(s)
'She sai"d, "Don't!'发布于 2015-11-16 15:02:12
我只是问自己python中的()方法在哪里?特别是Perl的qXXX糖。在urllib中找到了一个引号(),但这不是我想要的--就是简单地引用字符串本身中的字符串。然后它击中了我:
some_string = repr(some_string)重新配置内建将始终正确引用字符串。不过,它将被单独引用。Perl有双引号的倾向.
https://stackoverflow.com/questions/29559905
复制相似问题