Python 3.6.x字符串格式化方法小结

1  使用%符号进行格式

使用%符号进行字符串格式化的形式如下图所示,格式运算符%之前的部分为格式字符串,之后的部分为需要进行格式化的内容。


Python支持大量的格式字符,下表列出了比较常用的一部分。

格式字符

说明

%s

字符串 (采用str()的显示)

%r

字符串 (采用repr()的显示)

%c

单个字符

%d

十进制整数

%i

十进制整数

%o

八进制整数

%x

十六进制整数

%e

指数 (基底写为e)

%E

指数 (基底写为E)

%f、%F

浮点数

%g

指数(e)或浮点数 (根据显示长度)

%G

指数(E)或浮点数 (根据显示长度)

%%

字符%

使用这种方式进行字符串格式化时,要求被格式化的内容和格式字符之间必须一一对应。
>>> x = 1235
>>> so = "%o" % x
>>> so
'2323'
>>> sh = "%x" % x
>>> sh
'4d3'
>>> se = "%e" % x
>>> se
'1.235000e+03'

#等价于str()

>>> "%s" % 65
'65'
>>> "%s" % 65333
'65333'

#使用元组对字符串进行格式化,按位置进行对应
>>> '%d,%c' % (65, 65) 
'65,A'

#试图将字符串转换为整数进行输出,抛出异常
>>> "%d" % "555"
TypeError: %d format: a number is required, not str
>>> '%s' % [1, 2, 3]
'[1, 2, 3]'

#可以使用str()函数将任意类型数据转换为字符串
>>> str((1, 2, 3))
'(1, 2, 3)'
>>> str([1, 2, 3])
'[1, 2, 3]'

2  使用format()方法进行字符串格式化

除了上一节介绍的字符串格式化方法之外,目前Python社区更推荐使用format()方法进行格式化,该方法非常灵活,不仅可以使用位置进行格式化,还支持使用关键参数进行格式化,更妙的是支持序列解包格式化字符串,为程序员提供了非常大的方便。

在字符串格式化方法format()中可以使用的格式主要有b(二进制格式)、c(把整数转换成Unicode字符)、d(十进制格式)、o(八进制格式)、x(小写十六进制格式)、X(大写十六进制格式)、e/E(科学计数法格式)、f/F(固定长度的浮点数格式)、%(使用固定长度浮点数显示百分数)。Python 3.6.x开始支持在数字常量的中间位置使用单个下划线作为分隔符来提高数字可读性,相应的,字符串格式化方法format()也提供了对下划线的支持。下面的代码演示了其中的部分用法:
>>> 1/3
0.3333333333333333

#保留3位小数

>>> '{0:.3f}'.format(1/3)
0.333

#格式化为百分数
>>> '{0:%}'.format(3.5)
'350.000000%'

#Python 3.6.0及更高版本支持
>>> '{0:_},{0:_x}'.format(1000000)
'1_000_000,f_4240'

#Python 3.6.0及更高版本支持
>>> '{0:_},{0:_x}'.format(10000000)
'10_000_000,98_9680'
>>> print("The number {0:,} in hex is: {0:#x}, in oct is {0:#o}".format(55))
The number 55 in hex is: 0x37, in oct is 0o67
>>> print("The number {0:,} in hex is: {0:x}, the number {1} in oct is {1:o}".format(5555, 55))
The number 5,555 in hex is: 15b3, the number 55 in oct is 67
>>> print("The number {1} in hex is: {1:#x}, the number {0} in oct is {0:#o}".format(5555, 55))
The number 55 in hex is: 0x37, the number 5555 in oct is 0o12663
>>> print("my name is {name}, my age is {age}, and my QQ is {qq}".format(name = "Dong", qq = "1234567", age = 38))
my name is Dong, my age is 38, and my QQ is 1234567
>>> position = (5, 8, 13)

#使用元组同时格式化多个值
>>> print("X:{0[0]};Y:{0[1]};Z:{0[2]}".format(position))
X:5;Y:8;Z:13
>>> weather = [("Monday", "rain"), ("Tuesday", "sunny"), ("Wednesday", "sunny"), ("Thursday", "rain"), ("Friday", "Cloudy")]
>>> formatter = "Weather of '{0[0]}' is '{0[1]}'".format
>>> for item in map(formatter, weather):
       print(item)

上面最后一段代码也可以改为下面的写法:
>>> for item in weather:
       print(formatter(item)) 

运行结果为:
Weather of 'Monday' is 'rain'
Weather of 'Tuesday' is 'sunny'
Weather of 'Wednesday' is 'sunny'
Weather of 'Thursday' is 'rain'
Weather of 'Friday' is 'Cloudy'

3  格式化的字符串常量

从Python 3.6.x开始支持一种新的字符串格式化方式,官方叫做Formatted String Literals,其含义与字符串对象的format()方法类似,但形式更加简洁。
>>> name = 'Dong'
>>> age = 39
>>> f'My name is {name}, and I am {age} years old.'
'My name is Dong, and I am 39 years old.'
>>> width = 10
>>> precision = 4
>>> value = 11/3
>>> f'result:{value:{width}.{precision}}'
'result:     3.667'

4  使用Template模板进行格式化

Python标准库string还提供了用于字符串格式化的模板类Template,可以用于大量信息的格式化,尤其使用于网页模板内容的替换和格式化。例如:
>>> from string import Template

#创建模板

>>> t = Template('My name is ${name}, and is ${age} yeare old.')
>>> d = {'name':'Dong', 'age':39}

#替换

>>> t.substitute(d)
'My name is Dong, and is 39 yeare old.'
>>> tt = Template('My name is $name, and is $age yeare old.')
>>> tt.substitute(d)
'My name is Dong, and is 39 yeare old.'
>>> html = '''<html><head>${head}</head><body>${body}</body></html>'''
>>> t = Template(html)
>>> d = {'head':'test', 'body':'This is only a test.'}
>>> t.substitute(d)
'<html><head>test</head><body>This is only a test.</body></html>'

本文节选自《Python程序设计开发宝典》(董付国著,清华大学出版社,2017.8,ISBN:9787302472100)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dongfuguo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值