方法一:使用函数默认不带参数,则默认以空格填充(文字与空格总字符数等于输入的数字)
#代码
print("|","Ursula".ljust(20),"|") #左对齐
print("|","Ursula".center(20),"|") #居中对齐
print("|","Ursula".rjust(20),"|") #右对齐
#运行结果
| Ursula |
| Ursula |
| Ursula |
方法二:函数中带参数,则以参数作为填充字符
#代码
print("|","Ursula".ljust(20,"*"),"|") #左对齐
print("|","Ursula".center(20,"*"),"|") #居中对齐
print("|","Ursula".rjust(20,"*"),"|") #右对齐
#运行结果
| Ursula************** |
| *******Ursula******* |
| **************Ursula |
#代码
print("|",format("Ursula","*>20"),"|") #左对齐
print("|",format("Ursula","*^20"),"|") #居中对齐
print("|",format("Ursula","*<20"),"|") #右对齐
#运行结果
| **************Ursula |
| *******Ursula******* |
| Ursula************** |
参考:https://blog.csdn.net/beautiful77moon/article/details/88874541,仅作为个人笔记