Home  >  Article  >  Backend Development  >  How to align in python

How to align in python

(*-*)浩
(*-*)浩Original
2019-07-03 13:43:0811629browse

Printing is often used in python, but when printing tables, we often encounter Chinese and English alignment problems. How to solve it?

How to align in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn