相信很多人在格式化字符串的时候都用\"%s\" % v
的语法,PEP 3101 提出一种更先进的格式化方法 str.format()
并成为 Python 3 的标准用来替换旧的 %s
格式化语法,CPython 从 2.6 开始已经实现了这一方法(其它解释器未考证)。
新的 format()
方法其实更像是一个简略版的模板引起(Template Engine),功能非常丰富,官方文档对其语法的描述如下:
1234567891011
|
\”\”\”replacement_field ::= \”{\” [field_name] [\”!\” conversion] [\”:\” format_spec] \”}\”field_name ::= arg_name (\”.\” attribute_name | \”[\” element_index \”]\”)*arg_name ::= [identifier | integer]attribute_name ::= identifierelement_index ::= integer | index_stringindex_string ::= +conversion ::= \”r\” | \”s\” | \”a\”format_spec ::= \”\”\”pass # Donot output |
我将其准换成铁路图的形式,(可能)更直观一些:
模板中替换变量用 {}
包围,且由 :
分为两部分,其中后半部分 format_spec
在后面会单独讨论。前半部分有三种用法:
- 空
- 代表位置的数字
- 代表keyword的标识符
这与函数调用的参数类别是一致的:
123456
|
print(\”{} {}\”.format(\”Hello\”, \”World\”))# is equal to…print(\”{0} {1}\”.format(\”Hello\”, \”World\”))print(\”{hello} {world}\”.format(hello=\”Hello\”, world=\”World\”)) print(\”{0}{1}{0}\”.format(\”H\”, \”e\”)) |
1234
|
Hello WorldHello WorldHello WorldHeH |
除此之外,就像在0x05 函数参数与解包中提到的一样,format()
中也可以直接使用解包操作:
12
|
print(\”{lang}.{suffix}\”.format(**{\”lang\”: \”Python\”, \”suffix\”: \”py\”}))print(\”{} {}\”.format(*[\”Python\”, \”Rocks\”])) |
在模板中还可以通过 .identifier
和 [key]
的方式获取变量内的属性或值(需要注意的是 \"{}{}\"
相当于 \"{0}{1}\"
):
1234567
|
data = {\’name\’: \’Python\’, \’score\’: 100}print(\”Name: {0[name]}, Score: {0[score]}\”.format(data)) # 不需要引号 langs = [\”Python\”, \”Ruby\”]print(\”{0[0]} vs {0[1]}\”.format(langs)) print(\”n====nHelp(format):n {.__doc__}\”.format(str.format)) |
123456789
|
Name: Python, Score: 100Python vs Ruby ====Help体, Lato, \’Helvetica Neue\’, Helvetica, Arial, sans-serif;line-height: 1.5\”>的语法,PEP 3101 提出一种更先进的格式化方法 str.format() 并成为 Python 3 的标准用来替换旧的 %s 格式化语法,CPython 从 2.6 开始已经实现了这一方法(其它解释器未考证)。
新的 format() 方法其实更像是一个简略版的模板引起(Template Engine),功能非常丰富,官方文档对其语法的描述如下:
1234567891011
|
\”\”\”replacement_field ::= \”{\” [field_name] [\”!\” conversion] [\”:\” format_spec] \”}\”field_name ::= arg_name (\”.\” attribute_name | \”[\” element_index \”]\”)*arg_name ::= [identifier | integer]attribute_name ::= identifierelement_index ::= integer | index_stringindex_string ::= +conversion ::= \”r\” | \”s\” | \”a\”format_spec ::= \”\”\”pass # Donot output |
我将其准换成铁路图的形式,(可能)更直观一些:
模板中替换变量用 {} 包围,且由 : 分为两部分,其中后半部分 format_spec 在后面会单独讨论。前半部分有三种用法:
- 空
- 代表位置的数字
- 代表keyword的标识符
这与函数调用的参数类别是一致的:
123456
|
print(\”{} {}\”.format(\”Hello\”, \”World\”))# is equal to…print(\”{0} {1}\”.format(\”Hello\”, \”World\”))print(\”{hello} {world}\”.format(hello=\”Hello\”, world=\”World\”)) print(\”{0}{1}{0}\”.format(\”H\”, \”e\”)) |
1234
|
Hello WorldHello WorldHello WorldHeH |
除此之外,就像在0x05 函数参数与解包中提到的一样,format() 中也可以直接使用解包操作:
12
|
print(\”{lang}.{suffix}\”.format(**{\”lang\”: \”Python\”, \”suffix\”: \”py\”}))print(\”{} {}\”.format(*[\”Python\”, \”Rocks\”])) |
在模板中还可以通过 .identifier 和 [key] 的方式获取变量内的属性或值(需要注意的是 \"{}{}\" 相当于 \"{0}{1}\" ):
1234567
|
data = {\’name\’: \’Python\’, \’score\’: 100}print(\”Name: {0[name]}, Score: {0[score]}\”.format(data)) # 不需要引号 langs = [\”Python\”, \”Ruby\”]print(\”{0[0]} vs {0[1]}\”.format(langs)) print(\”n====nHelp(format):n {.__doc__}\”.format(str.format)) |
123456789
|
Name: Python, Score: 100Python vs Ruby ====Helpan>(format): S.format(*args, |
|