知行编程网知行编程网  2022-09-07 15:30 知行编程网 隐藏边栏  287 
文章评分 1 次,平均分 5.0
导语: 本文主要介绍了关于python怎么让数字右对齐?的相关知识,包括如何让公式居中编号右对齐,以及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

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享