python ljust,rjust,center,zfill對齊使用方法


字符串在輸出時的對齊:
S.ljust(width,[fillchar]) 
#輸出width個字符,S左對齊,不足部分用fillchar填充,默認的為空格。 
S.rjust(width,[fillchar]) #右對齊 
S.center(width, [fillchar]) #中間對齊 
S.zfill(width) #把S變成width長,並在右對齊,不足部分用0補足 

實例

 1 >>> str = "this is string example....wow!!!";
 2 >>> str.ljust(50,'0')
 3 'this is string example....wow!!!000000000000000000'
 4 >>> str.ljust(50)
 5 'this is string example....wow!!!                  '
 6 >>> str.rjust(50)
 7 '                  this is string example....wow!!!'
 8 >>> str.rjust(50,'0')
 9 '000000000000000000this is string example....wow!!!'
10 >>> str.center(50,'0')
11 '000000000this is string example....wow!!!000000000'
12 >>> str.center(50)
13 '         this is string example....wow!!!         '
14 >>> str.zfill(50)
15 '000000000000000000this is string example....wow!!!'
16 >>>

 


免責聲明!

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



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