python format格式化進階-左對齊右對齊 取位數


(1)< (默認)左對齊、> 右對齊、^ 中間對齊、= (只用於數字)在小數點后進行補齊

(2)取位數“{:4s}”、"{:.2f}"等

1 >>> print('{} and {}'.format('hello','world'))  # 默認左對齊
2 hello and world
3 >>> print('{:10s} and {:>10s}'.format('hello','world'))  # 取10位左對齊,取10位右對齊
4 hello      and      world
5 >>> print('{:^10s} and {:^10s}'.format('hello','world'))  # 取10位中間對齊
6   hello    and   world  
7 >>> print('{} is {:.2f}'.format(1.123,1.123))  # 取2位小數
8 1.123 is 1.12
9 >>> print('{0} is {0:>10.2f}'.format(1.123))  # 取2位小數,右對齊,取10位
10 1.123 is       1.12


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM