zfill()方法语法:
1 str.zfill(width)
- width -- 指定字符串的长度。原字符串右对齐,前面填充0。
1 #!/usr/bin/python 2 3 str = "this is string example....wow!!!"; 4 5 print str.zfill(40); 6 print str.zfill(50);
得到返回值
1 00000000this is string example....wow!!! 2 3 000000000000000000this is string example....wow!!!
另一种方法
1 # -*- coding:utf-8 -*- 2 3 str1 = 'testzfill' 4 5 print "str1:",str1 6 7 print "str1.rjust(30,'0'):" 8 print str1.rjust(30,'0') 9 print "str1.zfill(30):" 10 print str1.zfill(30) 11 12 print "str1.rjust(5,'0'):" 13 print str1.rjust(5,'0') 14 print "str1.zfill(5):" 15 print str1.zfill(5)
#得到返回值
1 str1: testzfill 2 str1.rjust(30,'0'): 3 000000000000000000000testzfill 4 str1.zfill(30): 5 000000000000000000000testzfill 6 str1.rjust(5,'0'): 7 testzfill 8 str1.zfill(5): 9 testzfill