format

Format String Syntax
********************

The "str.format()" method and the "Formatter" class share the same
syntax for format strings (although in the case of "Formatter",
subclasses can define their own format string syntax).  The syntax is
related to that of formatted string literals, but there are
differences.

Format strings contain "replacement fields" surrounded by curly braces
"{}". Anything that is not contained in braces is considered literal
text, which is copied unchanged to the output.  If you need to include
a brace character in the literal text, it can be escaped by doubling:
"{{" and "}}".

The grammar for a replacement field is as follows:

      replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
      field_name        ::= arg_name ("." attribute_name | "[" element_index "]")*
      arg_name          ::= [identifier | integer]
      attribute_name    ::= identifier
      element_index     ::= integer | index_string
      index_string      ::= <any source character except "]"> +
      conversion        ::= "r" | "s" | "a"
      format_spec       ::= <described in the next section>

In less formal terms, the replacement field can start with a
*field_name* that specifies the object whose value is to be formatted
and inserted into the output instead of the replacement field. The
*field_name* is optionally followed by a  *conversion* field, which is
preceded by an exclamation point "'!'", and a *format_spec*, which is
preceded by a colon "':'".  These specify a non-default format for the
replacement value.

See also the Format Specification Mini-Language section.

The *field_name* itself begins with an *arg_name* that is either a
number or a keyword.  If it's a number, it refers to a positional
argument, and if it's a keyword, it refers to a named keyword
argument.  If the numerical arg_names in a format string are 0, 1, 2,
... in sequence, they can all be omitted (not just some) and the
numbers 0, 1, 2, ... will be automatically inserted in that order.
Because *arg_name* is not quote-delimited, it is not possible to
specify arbitrary dictionary keys (e.g., the strings "'10'" or
"':-]'") within a format string. The *arg_name* can be followed by any
number of index or attribute expressions. An expression of the form
"'.name'" selects the named attribute using "getattr()", while an
expression of the form "'[index]'" does an index lookup using
"__getitem__()".

Changed in version 3.1: The positional argument specifiers can be
omitted, so "'{} {}'" is equivalent to "'{0} {1}'".

Some simple format string examples:

   "First, thou shalt count to {0}"  # References first positional argument
   "Bring me a {}"                   # Implicitly references the first positional argument
   "From {} to {}"                   # Same as "From {0} to {1}"
   "My quest is {name}"              # References keyword argument 'name'
   "Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
   "Units destroyed: {players[0]}"   # First element of keyword argument 'players'.

The *conversion* field causes a type coercion before formatting.
Normally, the job of formatting a value is done by the "__format__()"
-- More  --

格式字符串的语法
* * * * * * * * * * * * * * * * * * * *
“str.format()”方法和“Formatter”类共享相同的内容
格式字符串的语法(尽管在“格式化程序”的情况下,
子类可以定义自己的格式字符串语法)。语法
与格式化的字符串字面量相关,但是有
的差异。
格式字符串包含“替换字段”,周围环绕着花括号
“{ }”。任何不包含在括号里的东西都被认为是文字的
文本,它被复制到输出中。如果你需要包括
文字文本中的一个支撑字符,可以通过加倍来转义:
“{ {” and “} }”。
替换字段的语法如下:
replacement_field::= " { "[field_name][“!”转换][”:“format_spec]“}”
fieldname:=argname("。"attributename""elementindex")
argname::=标识符
attribute_name::=标识符
元素索引::=整数indexstring
索引字符串::=<任何源字符,除了“”
转换::="r"s"a"
格式规范::
在不太正式的术语中,替换字段可以从a开始
指定要格式化的对象的fieldname
然后插入到输出中而不是替换字段。那
fieldname是可选的,后面是一个转换字段,也就是
前面是一个感叹号“!”和一个格式规范,它是
在前面有一个冒号“':'”。它们为以下内容指定了非默认格式
替换值。
参见格式规范的迷你语言部分。
fieldname本身以一个argname开头
数字或字。如果它是一个数字,它指的是一个位置
参数,如果它是一个关键字,它指的是一个命名关键字
论点。如果格式字符串中的数字argname是0 1 2,
…依次,它们都可以被省略(不只是一些)和
数字0 1 2,……将自动插入到这个顺序中。
因为argname不是被引用的,所以不可能
指定任意的字典键(例如,字符串“'10'”或
“:-”)在一个格式字符串中。可以遵循任何一个名称
索引或属性表达式的数量。一种形式的表达
".name'"选择使用"getattr()"的命名属性,而一个
表单“索引”的表达式使用索引查找
“__getitem__()”。
在版本3.1中改变了:位置参数说明符可以是
省略了,所以“{ } { }”相当于“{ 0 } { 1 }”。
一些简单的格式字符串示例:
"首先,你要数到0"引用第一个位置参数
"给我一个"隐含地引用第一个位置参数
"从01"从01"
"我的任务是名字"引用关键字参数"名称"
“重量吨{ 0。第一个位置arg的重量“重量”属性
“单位被摧毁:玩家0”关键字参数“玩家”的第一个元素。
转换字段在格式化之前会导致类型强制。
通常,格式化一个值的工作是由“格式()”完成的。
——更多的

<模板字符串>.format(<逗号分隔的参数>)

{<参数序号> : <格式控制标记>}

:填充对齐宽度,. 精度类型
引导符号填充单个字符(必须与对齐,宽度连用)< : 左对齐 >: 右对齐 ^ : 居中对齐槽设定的输出宽度数字的千位分隔符浮点数小数精度 或 最大输出长度整数类型:b,c,d,o,x,X 浮点数类型:e,E,f,%
{0: }, {1: },{:10}{0:*20}无效;{0:*^20}有效
>> "{0:=^20}".format("PYTHON")
>> '=======PYTHON======='

>>"{0:*>20}".format("BIT")
>>'*****************BIT'

>>"{:10}".format("BIT")
>>'BIT       '

>>"{0:,.2f}".format(12345.6789)
>>'12,345.68'

>>"{0:b}, {0:d}, {0:o}, {0:x}, {0:X}".format(425)
>>'110101001, 425, 651, 1a9, 1A9'

>>"{0:e}, {0:E}, {0:f}, {0:%}".format(3.14)
>>'3.140000e+00, 3.140000E+00, 3.140000, 314.000000%'
记忆链:
    开头 --> 填充对齐宽度 --> ,,类型 
    2             3            3

位置映射:

如果不写参数序号参数序号,默认一个一个填
`print("{}:{}".format('192.168.0.100',8888`))

    `#192.168.0.100:8888`
关键字映射
print("{server}{1}:{0}".format(8888,'192.168.1.100',server='Web Server Info :'))
#Web Server Info :192.168.1.100:8888  
元素访问
print("{0[0]}.{0[1]}".format(('baidu','com')))
#baidu.com 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值