Contents

20.2.7. 字符串对齐

text = 'Hello World'
print(format(text, '>20'))
print(format(text, '<20'))
print(format(text, '^20'))
print(format(text, '=>20s'))
print(format(text, '*^20s'))
print()
print(text.rjust(20, "="))
print(text.center(20, "*"))

print("{:>10s}{:>10s}".format("Hello", "World"))

x = 1.2345
print(format(x, '>10'))
print(format(x, '^10.2f'))

输出信息

 1.2345
1.23