Python:字符串的格式化

Python 2.6:format函数

ython2.6时,出现了一种新的字符串格式化方式,str.format()函数,相比于%操作符,format函数使用{}和:代替了%,威力更加强大,在映射关系方面,format函数支持位置映射、关键字映射、对象属性映射、下标映射等多种方式,不仅参数可以不按顺序,也可以不用参数或者一个参数使用多次,下面通过几个例子来说明。

'{1} {0}'.format('abc', 123)  # 可以不按顺序进行位置映射,输出'123 abc'

'{} {}'.format('abc', 123)  # 可以不指定参数名称,输出'abc 123'

'{1} {0} {1}'.format('abc', 123)  # 参数可以使用多次,输出'123 abc 123'

'{name} {age}'.format(name='tom', age=27)  # 可以按关键字映射,输出'tom 27'

'{person.name} {person.age}'.format(person=person)  # 可以按对象属性映射,输出'tom 27'

'{0[1]} {0[0]}'.format(lst)  # 通过下标映射

可以看到,format函数比%操作符使用起来更加方便,不需要记住太多各种占位符代表的意义,代码可读性也更高。在复杂格式控制方面,format函数也提供了更加强大的控制方式:

[[填充字符]对齐方式][符号标志][#][宽度][,][.精度][类型]
在这里插入图片描述
例如:

‘{:S^+#016,.2f}’.format(1234) # 输出’SSS+1,234.00SSSS’

第三个参数 format_spec

语法格式如下:
[[fill]align][sign][#][0][width][grouping_option][.precision][type]

  • fill:在进行对齐操作的时候的填充的字符,可以是任意的。

  • align: “<” | “>” | “=” | “^” 对齐操作符,分别对应 字符串左对齐、字符串右对齐、数字填充、居中对齐

  • sign: “+” | “-” | " " 数字的时候符号的格式化样式

  • width :对齐的宽度

  • grouping_option : “_” | “,” 对数字按千的长度分组的标识

  • precision :精度:如浮点数后要显示几位小数

  • type :显示的类型,字符串的有两类,一个是s,一个None,都代表字符串,数字以下几类: “b” | “c” | “d” | “e” | “E” | “f” | “F” | “g” | “G” | “n” | “o” | “s” | “x” | “X” | “%”
    在这里插入图片描述

print('this left Aligned:{:<10}'.format('test')) #用空格填充,限定长度为10
print('this left Aligned:{:*<10}'.format('test')) #用*填充,限定长度为10
print('this left Aligned:{:1>10}'.format('test')) #用1填充,限定长度为20
print('this left Aligned:{:a^10}'.format('test')) #用a填充,限定长度为30
 
#数字的符号
print('The positive number is :{:d},The negetive number is :{:d}'.format(5,-5)) #正常显示 
print('The positive number is :{:+d},The negetive number is :{:+d}'.format(5,-5))#显式的显示正号
print('The positive number is :{:-d},The negetive number is :{:-d}'.format(5,-5))#正常显示
 
#数字的进制转换:
myNum=123456
print('int:{0:d},bin:{0:b},hex:{0:x},oct:{0:o}'.format(myNum))
#前面显示相应的标志符号:如0x,0o,0b等
print('int:{0:d},bin:{0:#b},hex:{0:#X},oct:{0:#o}'.format(myNum))
#千的分割
print('{0:,}'.format(myNum*1000))
# 123,456
print('{0:_}'.format(myNum*1000))
# 123_456
 
#表示百分数
print('{0:.2%}'.format(0.85))
# 85.00%
print('{0:.3%}'.format(3/4))
# 75.000%
 
# 表示一个数字的固定的长度
print("length is 5 :{0:05x},{0:0=5o},{0:05d},{0:#05o},{0:#05.1f}".format(123))
# length is 5 :0007b,00173,00123,0o173,123.0

Python 3.6:f-string

Python在3.6版本中也为我们带来了类似的功能:Formatted String Literals(字面量格式化字符串),简称f-string。

f-string就是以f’‘开头的字符串,类似u’‘和b’’,字符串内容和format方法中的格式一样,但是可以直接将变量带入到字符串中,可读性进一步增加,例如:

amount = 1234
f'请转账给我{amount:,.2f}元'  # '请转账给我1,234.00元'

同时,f-string的性能是比%和format都有提升的,我们做一个简单的测试,分别使用%操作符、format和f-string将下面语句执行10000次:

'My name is %s and i'm %s years old.' % (name, age)
'My name is {} and i'm {} years old.'.format(name, age)
f'My name is {name} and i'm {age} years old.'

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值