Python zfill()方法


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM