(format_spec) 语法格式:复制代码
- replacement_field ::= "{" [field_name] [debug] ["!" conversion] [":" format_spec] "}"
- field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
- arg_name ::= [identifier | digit+]
- attribute_name ::= identifier
- element_index ::= digit+ | index_string
- index_string ::= <any source character except "]"> +
- debug ::= "="
- conversion ::= "r" | "s" | "a"
- format_spec ::= format-spec:format_spec
笔者认为这样的语法格式还是很清晰的。至于具体每个缩写代表,笔者建议直接翻看文档,这里就不直接照抄了。常见的选项见多了就会了,见不到的人不会需要那些内容。复制代码
- format_spec ::= [[fill]align][sign]["z"]["#"]["0"][width][grouping_option]["." precision][type]
- fill ::= <any character>
- align ::= "<" | ">" | "=" | "^"
- sign ::= "+" | "-" | " "
- width ::= digit+
- grouping_option ::= "_" | ","
- precision ::= digit+
- type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
调用此方法的字符串可以包含字符串字面值或者以花括号 {} 括起来的替换域。
每个替换域可以包含一个位置参数的数字索引,或者一个关键字参数的名称。 返回的字符串副本中每个替换域都会被替换为对应参数的字符串值。
简单的来说就是复制代码
- >>>print('%(language)s has %(number)03d quote types.' %
- ... {'language': "Python", "number": 2})
- Python has 002 quote types.
- >>>print('%s has %03d quote types.' %("Python", 2))
- Python has 002 quote types.
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |