python中指定字符串宽度,并指定填充字符串

 

1、

>>> a = "good"
>>> a.ljust(1)
'good'
>>> a.ljust(10)   ## 左对齐
'good      '
>>> a.ljust(10,"-")  ## 左对齐,以-填充多余宽度
'good------'
>>> a.ljust(20,"x")
'goodxxxxxxxxxxxxxxxx'
>>> a
'good'
>>> a.center(2)
'good'
>>> a.center(10)  ## 中间对齐
'   good   '
>>> a.center(10,"x")
'xxxgoodxxx'
>>> a.rjust(3)
'good'
>>> a.rjust(10)   ##右对齐
'      good'
>>> a.rjust(10,"y")
'yyyyyygood'

 

posted @ 2021-02-25 12:22  小鲨鱼2018  阅读(1266)  评论(0编辑  收藏  举报