首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 3.6.x字符串格式化方法小结

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

作者头像
Python小屋屋主
发布2018-04-16 15:30:49
1.2K0
发布2018-04-16 15:30:49
举报
文章被收录于专栏:Python小屋Python小屋

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>'

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-08-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python小屋 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档