value = 4
space = 10
# move value to left
print(f'foo {value:<{space}} bar') # foo 4 bar
# move value to right
print(f'foo {value:>{space}} bar') # foo 4 bar
# center value
print(f'foo {value:^{space}} bar') # foo 4 bar
恒定空间:
value = 4
# move value to left
print(f'foo {value:<10} bar') # foo 4 bar
# move value to right
print(f'foo {value:>10} bar') # foo 4 bar
# center value
print(f'foo {value:^10} bar') # foo 4 bar
如果你想用其他char和space填充,请在开头指定它:
value = 4
space = 10
padd = '_'
print(f'foo {value:{padd}^{space}} bar') # foo ____4_____ bar
print(f'foo {value:_^10} bar') # foo ____4_____ bar