Python字符串方法详细介绍1_填充

来源:互联网 发布:新开淘宝怎样运营快速 编辑:程序博客网 时间:2024/06/06 05:57
1.填充center(width[, fillchar]) ,ljust(width[, fillchar]),rjust(width[, fillchar]),zfill(width),expandtabs([tabsize])* fillchar 参数指定了用以填充的字符,默认为空格(1)string.center(width[, fillchar]) 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串注意,当width-len(str)为奇数时,默认情况下左侧填充的字符要比右侧填充的字符要少。另外,如果字符串的长度比指定的位数要大时,该方法会返回原字符串例:
>>> print 'abc'.center(12)    abc>>> print 'abc'.center(12, '*')****abc*****>>> print 'abc'.center(2,'*')abc
2)string.ljust(width[, fillchar]) 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
>>> print 'abc'.ljust(12)abc>>> print 'abc'.ljust(12, '*')abc*********>>> print 'abc'.ljust(2,'*')abc
3)string.ljust(width[, fillchar]) 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
>>> print 'abc'.rjust(12)         abc>>> print 'abc'.rjust(12, '*')*********abc>>> print 'abc'.rjust(2,'*')abc
4)string.zfill(width) 返回长度为 width 的字符串,原字符串 string 右对齐,前面填充0
>>> print 'abc'.zfill(12)000000000abc>>> print 'abc'.zfill(2)abc
5)string.expandtabs([tabsize])的tabsize 参数默认为8。它的功能是把字符串中的制表符(tab)转换为适当数量的空格。
>>> print len('abc  ')5>>> print len('abc  '.expandtabs())5>>> print 'abc  '.expandtabs().replace(' ','*')abc**
问题:为什么制表符只有2个字符?说好的8个字符呢?难道是系统的原因?

0 0
原创粉丝点击