這篇文章主要介紹使用python3輸出print對(duì)齊的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
1.使用ljust,rjust,center方法
ljust,rjust,center這三個(gè)方法都可以設(shè)定對(duì)齊長(zhǎng)度,填充字符。
直接上測(cè)試代碼
text="我是字符串"; print(text); print(text.ljust(20)); print(text.rjust(20)); print(text.center(20)); print(text.ljust(20,'/')); print(text.rjust(20,'~')); print(text.center(20,"*"));
2.使用format方法
format方法同樣可以用來(lái)對(duì)齊字符串 <左對(duì)齊 >右對(duì)齊 ^中間對(duì)齊
text="我是字符串"; print(text); print(format(text,"<20")); print(format(text,">20")); print(format(text,"^20"));
以上是使用python3輸出print對(duì)齊的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!