天天看点

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,如需转载请自行联系原作者