Python---项目函数

zfill()方法:返回指定长度的字符串,原字符串右对齐,前面填充0。

str1 = "hello"
str2 = str1.zfill(3)
str3 = str1.zfill(8)
print str2        # 结果:hello
print str3        # 结果:000hello
str4 = str1.rjust(8, '0')
str5 = str1.rjust(8, 'a')
str6 = str1.rjust(3, '0')
print str4        # 结果:000hello
print str5        # 结果:aaahello
print str6        # 结果:hello

猜你喜欢

转载自blog.csdn.net/qq_34802511/article/details/87860480