天天看點

python中字元串的ljust、rjust、center方法講解

python中字元串的ljust、rjust、center方法講解

這三種方法的用法差不多:S.ljust(width[, fillchar]),即長度加占位符,預設為空格,這三種在格式化輸出時用着非常友善。

如:

>>> a="Hello world"

>>> print a.rjust(20)

'         Hello world'

>>> print a.ljust(20)

'Hello world         '

>>> print a.center(20)

'    Hello world     '

>>> print a.rjust(20,'*')

'*********Hello world'

>>> print a.ljust(20,'*')

'Hello world*********'

>>> print a.center(20,'*')

'****Hello world*****'

本文轉自 chengxuyonghu 51CTO部落格,原文連結:http://blog.51cto.com/6226001001/1765992,如需轉載請自行聯系原作者