在Python 3.6中,如果想要在格式化字符串的结果中包含一个反斜杠,请注意#1和#2会产生相同的不良结果,但#3会导致太多的反斜杠,另一个不良结果。
2
在Python 3.6中,如果想在格式化字符串的结果中包含一对反斜杠,请注意 #1 和 #2 会产生相同的不良结果,但是 #3 会导致过多的反斜杠,这也是我们不想要的结果。
t = "arst '{}' arst"
t.format(d)
>> "arst '2017-34-12' arst"
2
t = "arst \'{}\' arst"
t.format(d)
>> "arst '2017-34-12' arst"
3
t = "arst \\'{}\\' arst"
t.format(d)
>> "arst \\'2017-34-12\\' arst"
我希望您能够提供以下最终结果:
我期望的最终结果如下:
>> "arst \'2017-34-12\' arst"
repr()
,它显示了你需要在 Python 中键入的字符串,包括转义字符。实际的字符串只包含一个反斜杠,你可以通过print(t.format(d))
看到。 - kindall"arst '{}' arst,\"you said\"".format(d). Since this has a mixture of both
'and
"then escape is necessary. If all the inside are
'while the outside are
"` then there will be no need of escaping. Also if you all of them are the same, then it will converse one to the other so as it might be readable." - Onyambu