Python中ljust,rjust,center和zfill的用法解析

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

>>>s = 'this is a string.'
>>>s.ljust(50,"*") 'this is a string.*********************************'
>>>s.rjust(50, "*") '*********************************this is a string.'
>>>s.center(50, "*") '****************this is a string.*****************'
>>>s.zfill(50) '000000000000000000000000000000000this is a string.'
>>>len(s) 17

原字符串長度不變。spa

相關文章
相關標籤/搜索