创新互联Python教程:python怎么让数字右对齐?

python中使用format()方法格式化数字设置右对齐:< (默认)左对齐、> 右对齐、^ 中间对齐、= (只用于数字)在小数点后进行补齐

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


文章题目:创新互联Python教程:python怎么让数字右对齐?
转载源于:http://gydahua.com/article/dhjpdgd.html

扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流