方法一:直接用str的方法
str1 = 'format'
width = 30
# 字符串左侧对齐, 右侧补*:
print(str1.ljust(width, '*'))
# 字符串右侧对齐, 左侧补*:
print(str1.rjust(width, '*'))
# 字符串居中对齐, 两侧侧补*:
print(str1.center(width, '*'))
# 字符串右侧对齐, 左侧补0:
print(str1.zfill(width))
方法二:format
str1 = 'format'
width = 30
# 居中对齐,不足的用 = 补齐
f_str = '{:=^{w}}'.format(str1, w=width)
print(f_str)
# 其他对齐,不足用 * 补齐
print('2.{:*>30}'.format(str1)) #右对齐
print('3.{:*<30}'.format(str1)) #左对齐
参考:
https://blog.youkuaiyun.com/m0_46078030/article/details/103874056