若有补充,可以写在评论区
<,ljust:左对齐
>,rjust:右对齐
^,center:居中对齐
<, >, ^符号前面的符号为对齐时的填充符,默认为空格
# 字符串对齐
test = 'hello world'
print(test.ljust(20))
print(test.ljust(20, '*'))
print(test.rjust(20, '*'))
print(test.center(20, '*'))
print(test.center(20))
# format()
print('{:!<20}'.format(test))
print('{:!>20}'.format(test))
print('{:!^20}'.format(test))
# f-string
print(f'{test:@<20}')
print(f'{test:@>20}')
print(f'{test:@^20}')
# 占位符输出
print('%-20s' % test)
print('%20s' % test)
# format()使用
a = 'hello'
n = 5
print('{} {}'.format(a, n))
print('{0} {0}'.format(a, n)) # 输出:hello hello {}里面的数是索引
print('{n} {a}'.format(a=6, n='world')) # 关键字输出
# 输出结果
hello world
hello world*********
*********hello world
****hello world*****
hello world
hello world!!!!!!!!!
!!!!!!!!!hello world
!!!!hello world!!!!!
hello world@@@@@@@@@
@@@@@@@@@hello world
@@@@hello world@@@@@
hello world
hello world
hello 5
hello hello
world 6
输出结果: