目录
方法一,默认以空格填充(使用逗号连接字符串输出似乎有空格,加号没有)
方法 ljust(),rjust(),center() 分别实现居左、居右、居中操作
方法一,默认以空格填充
(使用逗号连接字符串输出似乎有空格,加号没有)
print('sun'.ljust(20))
print('sun'.rjust(20))
print('sun'.center(20))
print('|'+"sun".ljust(10)+'|'+"sun".rjust(10)+'|'+"sun".center(10)+"|")
sun
sun
sun
|sun | sun| sun |
方法二,以参数作为填充
print('sun'.ljust(20,'*'))
print('sun'.rjust(20,'*'))
print('sun'.center(20,'*'))
print('|'+"sun".ljust(10,'*')+'|'+"sun".rjust(10,'*')+'|'+"sun".center(10,'*')+"|")
sun*****************
*****************sun
********sun*********
|sun*******|*******sun|***sun****|
print('sun'.ljust(20,'*'))
print('sun'.rjust(20,'*'))
print('sun'.center(20,'*'))
print('|',"sun".ljust(10,'*'),'|',"sun".rjust(10,'*'),'|',"sun".center(10,'*'),"|")
sun*****************
*****************sun
********sun*********
| sun******* | *******sun | ***sun**** |
**使用format()函数格式化输出
print(format("sun","*>20"))
print(format("sun","*<20"))
print(format("sun","*^20"))
print(format("sun",">20"))
print(format("sun","<20"))
print(format("sun","^20"))
*****************sun
sun*****************
********sun*********
sun
sun
sun