python中的ljust、rjust

ljust()将字符串左对齐右侧填充

rjust()将字符串右对齐左侧填充

举个例子:

1 a = "hello world"
2 a1 = a.ljust(15, "*")
3 print(a1)

输出结果:

hello world****

我们定义了总长度为15,将字符串左对齐,剩余的右侧以*填充

1 b = "hello world"
2 b1 = b.rjust(15, "_")
3 print(b1)

输出结果:

____hello world

我们定义了总长度为15,将字符串右对齐,剩余的左侧以_填充

原文地址:https://www.cnblogs.com/zhangzengqiang/p/7524866.html