Python的格式化输出
参考
python的输出方式很多,这里整理一下
普通输出
x = 100
print(x)
print(‘hello, world’,’qxd’)
#效果
100
hello, world qxd
.format
{}用法
print(‘{} {}’.format(‘hello’,’world’)) # 不带字段
hello world
print(‘{0} {1}’.format(‘hello’,’world’)) # 带数字编号
hello world
print(‘{0} {1} {0}’.format(‘hello’,’world’)) # 打乱顺序
hello world hello
print(‘{1} {1} {0}’.format(‘hello’,’world’))
world world hello
print(‘{a} {tom} {a}’.format(tom=’hello’,a=’world’)) # 带关键字
world hello world
coord = {‘latitude’: ‘37.24N’, ‘longitude’: ‘-115.81W’} # 键值对替换
print(‘Coordinates: {latitude}, {longitude}’.format(**coord))
‘Coordinates: 37.24N, -115.81W’
‘Point({self.x}, {self.y})’.format(self=self) # 对象属性匹配
格式转换
0表示format中的位置,一个不写也可以
print(‘{0:b}’.format(3)) # ‘b’ - 二进制。将数字以2为基数进行输出。
11
print(‘{:c}’.format(20)) #’c’ - 字符。在打印之前将整数转换成对应的Unicode字符串。
print(‘{:d}’.format(20)) # ‘d’ - 十进制整数。将数字以10为基数进行输出。
20
print(‘{:o}’.format(20))# ‘o’ - 八进制。将数字以8为基数进行输出。
24
print(‘{:x}’.format(20)) # ‘x’ - 十六进制。将数字以16为基数进行输出,9以上的位数用小写字母。
14
print(‘{:e}’.format(20)) # ‘e’ - 幂符号。用科学计数法打印数字。用’e’表示幂。
2.000000e+01
print(‘{:g}’.format(20.1)) # ‘g’ - 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印。
20.1
print(‘{:f}’.format(20))
20.000000
print(‘{:n}’.format(20)) # ‘n’ - 数字。当值为整数时和’d’相同,值为浮点数时和’g’相同。不同的是它会根据区域设置插入数字分隔符。
20
print(‘{:%}’.format(20)) # ‘%’ - 百分数。将数值乘以100然后以fixed-point(‘f’)格式打印,值后面会有一个百分号。
2000.000000%
print(“int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}”.format(42)) # 在前面加“#”,则带进制前缀
‘int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010’
左中右对齐,位数补全
print(‘{} and {}’.format(‘hello’,’world’)) # 默认左对齐
hello and world
print(‘{:10s} and {:>10s}’.format(‘hello’,’world’)) # 取10位左对齐,取10位右对齐
hello and world
print(‘{:^10s} and {:^10s}’.format(‘hello’,’world’)) # 取10位中间对齐
hello and world
print(‘{} is {:.2f}’.format(1.123,1.123)) # 取2位小数
1.123 is 1.12
print(‘{0} is {0:>10.2f}’.format(1.123)) # 取2位小数,右对齐,取10位
1.123 is 1.12
print(‘{:<30}’.format(‘left aligned’)) # 左对齐
‘left aligned ‘
print(‘{:>30}’.format(‘right aligned’)) # 右对齐
‘ right aligned’
print(‘{:^30}’.format(‘centered’)) # 中间对齐
‘ centered ‘
print(‘{:*^30}’.format(‘centered’)) # 使用“*”填充
‘***********centered***********‘
print(‘{:0=30}’.format(11)) # 还有“=”只能应用于数字,这种方法可用“>”代替
‘000000000000000000000000000011’ #左中右对齐及位数补齐
正负号显示
>>> ‘{:+f}; {:+f}’.format(3.14, -3.14) # 总是显示符号
‘+3.140000; -3.140000’
>>> ‘{: f}; {: f}’.format(3.14, -3.14) # 若是+数,则在前面留空格
‘ 3.140000; -3.140000’
>>> ‘{:-f}; {:-f}’.format(3.14, -3.14) # -数时显示-,与’{:f}; {:f}’一致
‘3.140000; -3.140000’
正负符号显示 %+f, %-f, 和 % f的用法
时间
>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> ‘{:%Y-%m-%d %H:%M:%S}’.format(d)
‘2010-07-04 12:15:58’
表示金钱
>>> ‘{:,}’.format(1234567890)
‘1,234,567,890’
输出表格
表示占位符可以嵌套
width = 5
for num in range(5,12):
for base in ‘dXob’:
print(‘{0:{width}{base}}’.format(num, base=base, width=width), end=’ ‘)
print()
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
10 A 12 1010
11 B 13 1011
f-string
# a.format(b)
“{0} {1}”.format(“hello”,”world”)
‘hello world’
# f”xxxx”# 可在字符串前加f以达到格式化的目的,在{}里加入对象,此为format的另一种形式:
a = “hello”
b = “world”
f”{a} {b}”
print(‘hello world’)
name = ‘jack’
age = 18
sex = ‘man’
job = “IT”
salary = 9999.99
print(f’my name is {name.capitalize()}.’)
print(f’I am {age:*^10} years old.’)
print(f’I am a {sex}’)
print(f’My salary is {salary:10.3f}’)
# 结果
my name is Jack.
I am ****18**** years old.
I am a man
My salary is 9999.990
s-string和r-string
https://blog.csdn.net/weixin_42165585/article/details/80980739
