>>> r"what"ever"
SyntaxError: invalid syntax
>>> r"what\"ever"
'what\\"ever'
So how do we get the quote but not the slash?
那么我们如何得到报价而不是斜杠呢?
And please don't suggest r'what"ever'
because then the question just becomes how do we include both types of quotes?
请不要说r'是什么,因为然后问题就变成了我们如何包含这两种类型的引语?
相关的
6 个解决方案
#1
104
If you want to use double quotes in strings but not single quotes, you can just use single quotes as the delimiter instead:
如果你想在字符串中使用双引号而不是单引号,你可以使用单引号作为分隔符:
r'what"ever'
If you need both kinds of quotes in your string, use a triple-quoted string:
如果您的字符串中需要两种引号,请使用三引号:
r"""what"ev'er"""
If you want to include both kinds of triple-quoted strings in your string (an extremely unlikely case), you can't do it, and you'll have to use non-raw strings with escapes.
如果要在字符串中包含这两种三引号的字符串(一种非常不可能的情况),就不能这样做,并且必须使用带有转义的非原始字符串。
#2
7
Python has more than one way to do strings, the following string syntax would allow you to use double quotes:
Python有不止一种方法来执行字符串,下面的字符串语法将允许您使用双引号:
'''what"ever'''
#3
7
If you need any type of quoting (single, double, and triple for both) you can "combine"(0) the strings:
如果您需要任何类型的引用(单、双、三重),您可以“组合”(0)字符串:
>>> raw_string_with_quotes = r'double"' r"single'" r'''double triple""" ''' r"""single triple''' """
>>> print raw_string_with_quotes
double"single'double triple""" single triple'''
You may also "combine"(0) raw strings with non-raw strings:
您也可以“组合”(0)原始字符串与非原始字符串:
>>> r'raw_string\n' 'non-raw string\n'
'raw_string\\nnon-raw string\n'
(0): In fact the python parser joins the strings, it does not create multiple strings. If you add the "+" operator, then multiple strings are created and combined.
(0):实际上python解析器连接字符串,它不创建多个字符串。如果您添加“+”操作符,那么将创建并组合多个字符串。
#4
2
Nevermind, the answer is raw triple-quoted strings:
没关系,答案是原始的三引号字符串:
r"""what"ever"""
#5
1
Since I stumbled on this answer, and it greatly helped me, but I found a minor syntactic issue, I felt I should save others possible frustration. The triple quoted string works for this scenario as described, but note that if the " you want in the string occurs at the end of the string itself:
因为我无意中发现了这个答案,它帮了我很大的忙,但我发现了一个小的句法问题,我觉得我应该为别人节省一些可能的挫败感。这三个引用的字符串对这个场景的描述是这样的,但是请注意,如果“您想要的字符串出现在字符串本身的末尾:
somestr = """This is a string with a special need to have a " in it at the end""""
You will hit an error at execution because the """" (4) quotes in a row confuses the string reader, as it thinks it has hit the end of the string already and then finds a random " out there. You can validate this by inserting a space into the 4 quotes like so: " """ and it will not have the error.
您将在执行时遇到一个错误,因为行中的“”(4)引号迷惑了字符串读取器,因为它认为已经命中了字符串的末尾,然后在那里发现了一个随机的“out there”。您可以通过在4个引号中插入空格来验证这一点,如so:“”,这样就不会出现错误。
In this special case you will need to either use:
在这种特殊情况下,您需要使用:
somestr = 'This.....at the end"'
or use the method described above of building multiple strings with mixed " and ' and then concatenating them after the fact.
或者使用上面描述的方法,用混合的“and”构建多个字符串,然后将它们连接在一起。
#6
-3
dqote='"'
sqote="'"
use the '+' operator and dqote
and squote
variables to get what you need
使用'+'运算符和dqote和squote变量来得到您需要的。
if I wantsed -e s/",u'"/",'"/g -e s/^"u'"/"'"/
you can try the following
如果我想要- e s /“u”/“,”/ g - e s / ^“u”/“”/你可以试试以下
dqote='"'
sqote="'"
cmd1="sed -e s/"+dqote+",u'"+dqote+"/"+dqote+",'"+dqote+'/g -e s/^"u'+sqote+dqote+'/'+dqote+sqote+dqote+'/'
#1
104
If you want to use double quotes in strings but not single quotes, you can just use single quotes as the delimiter instead:
如果你想在字符串中使用双引号而不是单引号,你可以使用单引号作为分隔符:
r'what"ever'
If you need both kinds of quotes in your string, use a triple-quoted string:
如果您的字符串中需要两种引号,请使用三引号:
r"""what"ev'er"""
If you want to include both kinds of triple-quoted strings in your string (an extremely unlikely case), you can't do it, and you'll have to use non-raw strings with escapes.
如果要在字符串中包含这两种三引号的字符串(一种非常不可能的情况),就不能这样做,并且必须使用带有转义的非原始字符串。
#2
7
Python has more than one way to do strings, the following string syntax would allow you to use double quotes:
Python有不止一种方法来执行字符串,下面的字符串语法将允许您使用双引号:
'''what"ever'''
#3
7
If you need any type of quoting (single, double, and triple for both) you can "combine"(0) the strings:
如果您需要任何类型的引用(单、双、三重),您可以“组合”(0)字符串:
>>> raw_string_with_quotes = r'double"' r"single'" r'''double triple""" ''' r"""single triple''' """
>>> print raw_string_with_quotes
double"single'double triple""" single triple'''
You may also "combine"(0) raw strings with non-raw strings:
您也可以“组合”(0)原始字符串与非原始字符串:
>>> r'raw_string\n' 'non-raw string\n'
'raw_string\\nnon-raw string\n'
(0): In fact the python parser joins the strings, it does not create multiple strings. If you add the "+" operator, then multiple strings are created and combined.
(0):实际上python解析器连接字符串,它不创建多个字符串。如果您添加“+”操作符,那么将创建并组合多个字符串。
#4
2
Nevermind, the answer is raw triple-quoted strings:
没关系,答案是原始的三引号字符串:
r"""what"ever"""
#5
1
Since I stumbled on this answer, and it greatly helped me, but I found a minor syntactic issue, I felt I should save others possible frustration. The triple quoted string works for this scenario as described, but note that if the " you want in the string occurs at the end of the string itself:
因为我无意中发现了这个答案,它帮了我很大的忙,但我发现了一个小的句法问题,我觉得我应该为别人节省一些可能的挫败感。这三个引用的字符串对这个场景的描述是这样的,但是请注意,如果“您想要的字符串出现在字符串本身的末尾:
somestr = """This is a string with a special need to have a " in it at the end""""
You will hit an error at execution because the """" (4) quotes in a row confuses the string reader, as it thinks it has hit the end of the string already and then finds a random " out there. You can validate this by inserting a space into the 4 quotes like so: " """ and it will not have the error.
您将在执行时遇到一个错误,因为行中的“”(4)引号迷惑了字符串读取器,因为它认为已经命中了字符串的末尾,然后在那里发现了一个随机的“out there”。您可以通过在4个引号中插入空格来验证这一点,如so:“”,这样就不会出现错误。
In this special case you will need to either use:
在这种特殊情况下,您需要使用:
somestr = 'This.....at the end"'
or use the method described above of building multiple strings with mixed " and ' and then concatenating them after the fact.
或者使用上面描述的方法,用混合的“and”构建多个字符串,然后将它们连接在一起。
#6
-3
dqote='"'
sqote="'"
use the '+' operator and dqote
and squote
variables to get what you need
使用'+'运算符和dqote和squote变量来得到您需要的。
if I wantsed -e s/",u'"/",'"/g -e s/^"u'"/"'"/
you can try the following
如果我想要- e s /“u”/“,”/ g - e s / ^“u”/“”/你可以试试以下
dqote='"'
sqote="'"
cmd1="sed -e s/"+dqote+",u'"+dqote+"/"+dqote+",'"+dqote+'/g -e s/^"u'+sqote+dqote+'/'+dqote+sqote+dqote+'/'