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

相关文章
相关标签/搜索