当前位置: 首页 > 图灵资讯 > 行业资讯> python怎么让数字右对齐?

python怎么让数字右对齐?

发布时间:2026-01-23 21:43:52

format()格式化数字设置在python中右对齐:< (默认)左对齐,> 右对齐、^ 中间对齐、= 小数点后补充(仅用于数字)

>>> 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知识。

相关文章

python怎么让数字右对齐?

python怎么让数字右对齐?

2026-01-23
python是一种编程语言吗?

python是一种编程语言吗?

2026-01-23
python怎么画pr曲线?

python怎么画pr曲线?

2026-01-23
python什么等价于True?

python什么等价于True?

2026-01-23
python怎么过滤掉空行?

python怎么过滤掉空行?

2026-01-23
python如何取余和取商?

python如何取余和取商?

2026-01-23