新聞中心
python中使用format()方法格式化數(shù)字設(shè)置右對齊:< (默認(rèn))左對齊、> 右對齊、^ 中間對齊、= (只用于數(shù)字)在小數(shù)點后進(jìn)行補齊

>>> print('{} and {}'.format('hello','world')) # 默認(rèn)左對齊
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位小數(shù)
1.123 is 1.12
>>> print('{0} is {0:>10.2f}'.format(1.123)) # 取2位小數(shù),右對齊,取10位
1.123 is 1.12
當(dāng)前題目:創(chuàng)新互聯(lián)Python教程:python怎么讓數(shù)字右對齊?
新聞來源:http://www.dlmjj.cn/article/dhjpdgd.html


咨詢
建站咨詢
