python左右同时对齐_python怎么对齐
在python中經(jīng)常會(huì)使用到打印,但是在打印表格時(shí)經(jīng)常遇到中英文對齊問題,怎么解決呢?
往往要對輸出內(nèi)容進(jìn)行對齊,看起來更清爽。(推薦學(xué)習(xí):Python視頻教程)
通過ljust(),center(),rjust()函數(shù)實(shí)現(xiàn)輸出的字符串左對齊、居中、右對齊的三種方式對齊。
使用函數(shù)默認(rèn)不帶參數(shù),則默認(rèn)以空格填充(文字與空格總字符數(shù)等于輸入的數(shù)字)#代碼
print("|","Ursula".ljust(20),"|") #左對齊
print("|","Ursula".center(20),"|") #居中對齊
print("|","Ursula".rjust(20),"|") #右對齊
#運(yùn)行結(jié)果
| Ursula |
| Ursula |
| Ursula |
通過format()函數(shù)格式化實(shí)現(xiàn)左對齊、居中、右對齊#代碼
print("|",format("Ursula","*>20"),"|") #左對齊
print("|",format("Ursula","*^20"),"|") #居中對齊
print("|",format("Ursula","*<20"),"|") #右對齊
#運(yùn)行結(jié)果
| **************Ursula |
| *******Ursula******* |
| Ursula************** |
更多Python相關(guān)技術(shù)文章,請?jiān)L問Python教程欄目進(jìn)行學(xué)習(xí)!
總結(jié)
以上是生活随笔為你收集整理的python左右同时对齐_python怎么对齐的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python和javascript哪个好
- 下一篇: python实现排序算法_数据结构之(3