创新互联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://hxwzsj.com/article/dhjpdgd.html

其他资讯

Copyright © 2025 青羊区翔捷宏鑫字牌设计制作工作室(个体工商户) All Rights Reserved 蜀ICP备2025123194号-14
友情链接: 手机网站制作 重庆网站建设 外贸网站设计方案 成都网站建设 成都企业网站建设公司 泸州网站建设 重庆网站建设 营销网站建设 成都网站建设 app网站建设 成都网站制作 网站制作 响应式网站设计 成都定制网站建设 成都网站设计公司 网站建设公司 成都网站建设 企业网站制作 品牌网站建设 定制网站制作 成都响应式网站建设公司 营销型网站建设