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個字符呢?難道是系統的原因?