python字符串对齐与填充

s = "abc"

# 从左到右填充:abc*****
# 从右到左填充:*****abc
# 从中间向两边填充:**abc***

# 方法1
print(s.ljust(8, "*"))
print(s.rjust(8, "*"))
print(s.center(8, "*"))

# 方法2
print(format(s, "*<8"))
print(format(s, "*>8"))
print(format(s, "*^8"))

posted @ 2022-10-24 11:24  随风飘-挨刀刀  阅读(54)  评论(0编辑  收藏  举报
Top