Python字符串str.rjust()方法

转载自:https://www.yiibai.com/python/string_rjust.html

示例

以下示例显示了rjust()方法的用法

例1:

str = "this is string example....wow!!!"
print (str.rjust(50, '*'))

当运行上面的程序,它产生以下结果

******************this is string example....wow!!!

例2:

str.rjust(width, fillchar)
print("aa".rjust(8))

结果

      aa

作用

使用指定的fillchar(不填则为空格)填充长度成为width的字符串右对齐的字符串,并返回。
如果宽度小于str.len,则返回原始字符串。

例3:

print("作业ID".rjust(8))
print("1".rjust(8))
    作业ID
           1

把汉字字符也当作一个字符占位,英文字符当作一个字符,所以容易对不准上下。

猜你喜欢

转载自blog.csdn.net/qq_38786209/article/details/80208872