格式化输出--对齐及补全
生活随笔
收集整理的這篇文章主要介紹了
格式化输出--对齐及补全
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自https://www.cnblogs.com/qinchao0317/p/10699717.html
左中右對齊及位數補全
(1)< (默認)左對齊、> 右對齊、^ 中間對齊、= (只用于數字)在小數點后進行補齊
(2)取位數“{:4s}”、"{:.2f}"等
>>> 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>>> '{:<30}'.format('left aligned') # 左對齊 'left aligned ' >>> '{:>30}'.format('right aligned') # 右對齊 ' right aligned' >>> '{:^30}'.format('centered') # 中間對齊 ' centered ' >>> '{:*^30}'.format('centered') # 使用“*”填充 '***********centered***********' >>>'{:0=30}'.format(11) # 還有“=”只能應用于數字,這種方法可用“>”代替 '000000000000000000000000000011' 左中右對齊及位數補齊總結
以上是生活随笔為你收集整理的格式化输出--对齐及补全的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ROC曲线 vs Precision-R
- 下一篇: 735. 行星碰撞