Python中print输入打印左对齐,右对齐,居中对齐等

print输出格式总结

通过使用ljust(),center(),rjust()函数来实现输入字符串的左右对齐,居中,右对齐等操作;
方法一:(函数不带参数,则默认以空格填充,注意:文字与空格总字符数等于输入的数字)
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************** |

你可能感兴趣的:(Python)