python3 字符串格式化format

 今天深入学习并记录一下python3的字符串格式化的语法知识,希望未来能写出更加简洁的代码。

(资料来源:Python学习手册)


形式化结构

如下格式化字符串中作为替代目标的形式化结构:

{fieldname!conversionflag:formatspec}

  • fieldname: 指定参数的一个数字或者关键字,后面跟着可选的".name" 或 "[index]"成分引用
  • conversionflag:可以是r、s、或者a分别是在该值上对repr、str、ascii内置函数的一次调用
  • formatspec:
    • 指定如何表示该值,包括字段长度、对齐方式、补零、小数点精度等细节,并且以一个可选的数据类型编码结束
    • 其形式上的描述如下(方括号表示可选的组成、并且不能编写为常量):
      • 格式:[[fill]align][sign][#][0][width][.precision][typecode]
      • align可以为:<(左对齐)、>(右对齐)、=(1个标记符后的补充)、^(居中对齐)
    • 它也可以包含嵌套的、只带有{}的格式化字符串,它从参数列表动态获取值

使用示例

fieldname 

指定参数的一个数字或者关键字,后面跟着可选的".name" 或 "[index]"成分引用

>>> import sys
>>> 'My {1[spam]} runs {0.platform}'.format(sys, {'spam': 'laptoop'})
'My laptoop runs darwin'

>>> somelist = list("SPAM")
>>> 'first={0[0]}, second={0[1]}'.format(somelist)
'first=S, second=P'

>>> parts=somelist[0],somelist[-1],somelist[1:3]
>>> parts
('S', 'M', ['P', 'A'])
>>> 'first={0}, last={1}, middle={2}'.format(*parts)
"first=S, last=M, middle=['P', 'A']"

>>> template='{motto}, {pork} and {food}'
>>> template.format(**{"motto": "spam", "food": "eggs", "pork": "ham"})
'spam, ham and eggs'

conversionflag 

这里以a为例,在所替换的值上对ascii内置函数的一次调用,如下:

 >>> hello = "你好"
>>> '{0!a}'.format(hello)
"'\\u4f60\\u597d'"
>>> '{!a}'.format(hello)
"'\\u4f60\\u597d'"
>>> '{}'.format(hello)
'你好'
>>> '{0}'.format(hello)
'你好'

formatspec

指定如何表示该值,包括字段长度、对齐方式、补零、小数点精度等细节,并且以一个可选的数据类型编码结束

指定对齐方式 +  字符宽度

从如下示例可看出, : 后的数字表示字符宽度,>表示右对齐,<、不设置表示左对齐

>>> '{0:10} = {1:10}'.format("spam", 123.4567)
'spam       =   123.4567'
>>> '{0:>10} = {1:<10}'.format("spam", 123.4567)
'      spam = 123.4567  '
>>> '{0:<10} = {1:10}'.format("spam", 123.4567)
'spam       =   123.4567'
>>> '{0:>10} = {1:10}'.format("spam", 123.4567)
'      spam =   123.4567'

>>> '{0:10} = {1:10}'.format("spam", 123.4567)
'spam       =   123.4567'
>>> '{0:10} = {1:=10}'.format("spam", 123.4567)
'spam       =   123.4567'

注意:= 不能用于字符串类型,如下报错:ValueError: '=' alignment not allowed in string format specifier

指定精度

>>> '{0:>10} = {1:10.2}'.format("spam", 123.4567)
'      spam =    1.2e+02'
>>> '{0:>10} = {1:10.2f}'.format("spam", 123.4567)
'      spam =     123.46'
>>> '{0:>10} = {1:10.2e}'.format("spam", 123.4567)
'      spam =   1.23e+02'
>>> '{0:>10} = {1:10.3e}'.format("spam", 123.4567)
'      spam =  1.235e+02'

数字指定进制

>>> '{0:>10} = {1:10}'.format("spam", 14)
'      spam =         14'
>>> '{0:>10} = {1:10b}'.format("spam", 14)
'      spam =       1110'
>>> '{0:>10} = {1:10X}'.format("spam", 14)
'      spam =          E'
>>> '{0:>10} = {1:10o}'.format("spam", 14)
'      spam =         16'

补零

>>> '{0:>10} = {1:>10.2f}'.format("spam", 123.4567)
'      spam =     123.46'
>>> '{0:>10} = {1:>010.2f}'.format("spam", 123.4567)
'      spam = 0000123.46'
>>> '{0:>10} = {1:<010.2f}'.format("spam", 123.4567)
'      spam = 123.460000'

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值