词条信息

admin
超级管理员
版本创建者 发短消息   
简易百科旧版 >> Python补齐字符串长度的实例 >> 历史版本

最新历史版本 :Python补齐字符串长度的实例 返回词条



ljust(len,str)字符向左对齐,用str补齐长度
 
rjust(len,str)字符向右对齐,用str补齐长度
 
rjust(len,str)字符中间对齐,用str补齐长度
 
 
print 'bbb'.ljust(10,'a')
 
输出:bbbaaaaaaa
 
print 'bbb'.rjust(10,'a')
 
输出:aaaaaaabbb
 
print 'bbb'.center(10,'a')
 
输出:aaabbbaaaa
 
 
zfill(width)指定字符串长度,右对齐,前面补充0
 
 
print '2'.zfill(5)
 
输出:00002