Python 字符串格式化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/543399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:21:04  来源:igfitidea点击:

Python string formatting

python

提问by localhost

I see you guys using

我看到你们在使用

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"

Is it advanced Python? Is there a tutorial for it? How can I learn about it?

是高级 Python 吗?有它的教程吗?我怎样才能了解它?

回答by dwc

It's common string formatting, and very useful. It's analogous to C-style printf formatting. See String Formatting Operationsin the Python.org docs. You can use multiple arguments like this:

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

"%3d\t%s" % (42, "the answer to ...")

回答by Dan Lew

That line of code is using Python string formatting. You can read up more on how to use it here: http://docs.python.org/library/stdtypes.html#string-formatting

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

回答by Andrea Ambu

It is not advanced, you can use ' or " to define a string.

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

Check the documentation.

检查文档