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補足 
post

 
 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 >>>
相關文章
相關標籤/搜索