Python:如何转义“%s" [英] Python: how to escape "%s"
本文介绍了Python:如何转义“%s"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一个包含%s"且没有格式的字符串
I'm trying to make a string that includes a "%s" with no formatting
例如,我想要这个:
#I want to make mystring = "x %s"
first_value = "x"
mystring = "%s %s" % first_value
所以,我想格式化第一个%s,但将第二个%s留在字符串中.当然,上面的代码给了我一个"TypeError:格式化字符串的参数不足"
So, I want to format the first %s, but leave the second %s in the string.. Of course the above code gives me a "TypeError:not enough arguments for format string"
有没有一种方法可以做到这一点?
Is there a way to achieve this in one line?
谢谢
我知道我能做到
mystring = "%s %s" % (first_value, "%s")
但是这种外观很难看.是否有更好的方法?
but this kind of looks ugly.. Is there a better way?
推荐答案
您可以通过将字符串中的%
字符加倍来对其进行转义:
You can escape a %
character in a format string by doubling it:
>>> "%s %%s" % "x"
"x %s"
这篇关于Python:如何转义“%s"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文