Python字符串格式

loc*_*ost 5 python

我看到你们正在使用

url = '"%s"' % url # This part

>>> url = "http://www.site.com/info.xx"
>>> print url
http://www.site.com/info.xx
>>> url = '"%s"' % url
>>> print url
"http://www.site.com/info.xx"
Run Code Online (Sandbox Code Playgroud)

它是高级Python吗?有没有它的教程?我该如何了解它?

dwc*_*dwc 16

这是常见的字符串格式,非常有用.它类似于C风格的printf格式.请参阅Python.org文档中的字符串格式化操作.你可以使用这样的多个参数:

"%3d\t%s" % (42, "the answer to ...")
Run Code Online (Sandbox Code Playgroud)

  • 真正的答案是43,但不要告诉任何人. (4认同)

Dan*_*Lew 8

那行代码使用的是Python字符串格式.您可以在此处阅读有关如何使用它的更多信息:http://docs.python.org/library/stdtypes.html#string-formatting


And*_*mbu 0

它并不高级,您可以使用 ' 或 " 来定义字符串。

检查文档