python format格式化进阶-左对齐右对齐 取位数

(1)< (默认)左对齐、> 右对齐、^ 中间对齐、= (只用于数字)在小数点后进行补齐

(2)取位数“{:4s}”、"{:.2f}"等

1 >>> print('{} and {}'.format('hello','world'))  # 默认左对齐
2 hello and world
3 >>> print('{:10s} and {:>10s}'.format('hello','world'))  # 取10位左对齐,取10位右对齐
4 hello      and      world
5 >>> print('{:^10s} and {:^10s}'.format('hello','world'))  # 取10位中间对齐
6   hello    and   world  
7 >>> print('{} is {:.2f}'.format(1.123,1.123))  # 取2位小数
8 1.123 is 1.12
9 >>> print('{0} is {0:>10.2f}'.format(1.123))  # 取2位小数,右对齐,取10位
10 1.123 is       1.12

猜你喜欢

转载自www.cnblogs.com/nuonuozhou/p/11298374.html
今日推荐