字符串格式化

导文

在字符串格式化中目前即将要远离printf格式的输出,使用逐步流行及进化的format函数,format函数是在printf的基础之上加以进化后的版本,功能强大使用方便。

格式化语法

format函数格式字符串语法
- “{}{xxx}”.format(*args,**kwargs) -> str
- args是位置参数,是一个元组
- kwargs是关键字参数,是一个字典
- 花括号表示占位符
- {}表示按照顺序匹配位置参数,{n}表示取位置参数索引为n的值
- {xxx}表示在关键参数中搜索名称一致的,即从关键参数字典中选取的值
- {{}}表示打印花括号,相当于转义
6.1.3.2. Format examples中有这么一句话
In most of the cases the syntax is similar to the old %-formatting, with the addition of the {} and with : used instead of %. For example, ‘%03.2f’ can be translated to ‘{:03.2f}’. 用:替代%号,因此将某一个数进行修饰时通过{:}来表示修饰,这个需要在format函数中格外注意
因此可知:后面是修饰符0表示填充,具体修饰符含义如下表示
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
fill ::=
align ::= “<” | “>” | “=” | “^”
sign ::= “+” | “-” | ” ”
width ::= integer
precision ::= integer
type ::= “b” | “c” | “d” | “e” | “E” | “f” | “F” | “g” | “G” | “n” | “o” | “s” | “x” | “X” | “%”
第一个参数为:fill填充字段
第二个参数为:左右或居中对齐
第三个参数为:标记正负数的
第四个参数为:#号表示显示转化为二进制,八进制或着十六进制0b,0o,0x
第五个参数为:当位数不够时用0来填充,相当于第一个字段any character为0
第六个参数为:行宽度
第七个参数为:逗号表示千分为用逗号分开用于整数,一般财务数据遇到
第八个参数为:数据的精度
第九个参数为:数据的输出类型

位置参数

代码:

#!/bin/python3
#-*- coding: UTF-8 -*-
print("{}:{}".format('192.168.1.100',8888))
print("{1}:{0}".format('8888','192.168.1.100'))
运行结果:
192.168.1.100:8888
192.168.1.100:8888

关键字参数或命名参数

代码

#!/bin/python3
#-*- coding: UTF-8 -*-
print("{server} {1}:{0}".format('8888','192.168.1.100',server='Web Server Info:'))
运行结果:
Web Server Info: 192.168.1.100:8888

访问元素

通过访问元组中的
代码

#!/bin/python3
#-*- coding: UTF-8 -*-
print("{0[1]}.{0[0]}".format(('8888','192.168.1.100')))
运行结果:
192.168.1.100.8888

对象属性访问

代码

#!/bin/python3
#-*- coding: UTF-8 -*-
from collections import namedtuple
Point = namedtuple('Point','x y')
p = Point(4,5)
print("{{{0.x},{0.y}}}".format(p))

对齐

什么叫对齐的偏移量,在对齐中偏移量通常表示某个字符串或结果共计占用多少个字符,至于左对齐或者右对齐也是在字符偏移量范围之内进行左右对齐
align[əˈlaɪn]
通常对齐中有如下选项:
- 左对齐<,如<10,表示总共偏移10个字符位,在这10个字符位中进行左对齐
- 右对齐>,如>10,表示总共偏移10个字符位,在这10个字符位中进行右对齐

代码

[python@centos7 329]$cat align.py 
#!/bin/python3
#-*- coding: UTF-8 -*-
print("{0}*{1}={2:2}".format(3,2,2*3),end=' ') 
print("{0}*{1}={2:<2}".format(3,2,2*3),end=' ')
print("{0}*{1}={2:>2}".format(3,2,2*3),end=' ')
print("{0}*{1}={2:<2}".format(3,2,2*3))
[python@centos7 329]$./align.py 
3*2= 6 3*2=6  3*2= 6 3*2=6
[python@centos7 329]$hexdump -C 1
00000000  33 2a 32 3d 20 36 20 33  2a 32 3d 36 20 20 33 2a  |3*2= 6 3*2=6  3*|
00000010  32 3d 20 36 20 33 2a 32  3d 36 20 0a              |2= 6 3*2=6 .|
0000001c

代码2:

#!/bin/python3
#-*- coding: UTF-8 -*-
a = "{0}*{1}={2:<2}".format(3,2,2*3)
b = "{0}*{1}={2:<2}".format(3,2,2*3)
c = "{0}*{1}={2:>2}".format(3,2,2*3)
d = "{:^30}".format('centered')
e = "{:*^30}".format('centered')
print(a)
print(b)
print(c)
print(d)
print(e)
运行结果:
3*2=6 
3*2=6 
3*2= 6
           centered           
***********centered***********

进制输出类型

代码:

#!/bin/python3
a="int:{0:d}; hex:{0:x}; oct:{0:o}; bin:{0:b}".format(42)
b="int:{0:d}; hex:{0:#x}; oct:{0:#o}; bin:{0:#b}".format(42)
print(a)
print(b)
运行结果:
int:42; hex:2a; oct:52; bin:101010
int:42; hex:0x2a; oct:0o52; bin:0b101010

宽度打印

#!/bin/python3
#-*- coding: UTF-8 -*-
octets = [192,168,0,1]
e = "{:02X}{:02X}{:02X}{:02X}".format(*octets)
print(e)
运行结果:
[python@centos7 330]$./octers.py 
C0A80001
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值