日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

格式化输出--对齐及补全

發布時間:2025/1/21 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 格式化输出--对齐及补全 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載自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' 左中右對齊及位數補齊

總結

以上是生活随笔為你收集整理的格式化输出--对齐及补全的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。