Printing is often used in python, but when printing tables, we often encounter Chinese and English alignment problems. How to solve it?
It is often necessary to align the output content to make it look cleaner. (Recommended learning: Python video tutorial)
Use the ljust(), center(), rjust() functions to realize the three output string alignments: left-aligned, centered, and right-aligned. aligned in a way.
If the function is used without parameters by default, it will be filled with spaces by default (the total number of characters of text and spaces is equal to the number entered)
#代码 print("|","Ursula".ljust(20),"|") #左对齐 print("|","Ursula".center(20),"|") #居中对齐 print("|","Ursula".rjust(20),"|") #右对齐 #运行结果 | Ursula | | Ursula | | Ursula |
Formatting through the format() function Achieve left alignment, center, right alignment
#代码 print("|",format("Ursula","*>20"),"|") #左对齐 print("|",format("Ursula","*^20"),"|") #居中对齐 print("|",format("Ursula","*<20"),"|") #右对齐 #运行结果 | **************Ursula | | *******Ursula******* | | Ursula************** |
For more Python related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to align in python. For more information, please follow other related articles on the PHP Chinese website!