ljust,center 和 rjust

str.ljust 左对齐

字符串方法 str.ljust(),Python 官方文档描述如下:

help(str.ljust)
Help on method_descriptor:

ljust(self, width, fillchar=' ', /)
    Return a left-justified string of length width.
    
    Padding is done using the specified fill character (default is a space).

返回长度为 width 的字符串,原字符串在其中靠左对齐。使用指定的 fillchar 填充空位 (默认使用 ASCII 空格符)。如果 width 小于等于字符串长度 len(str) 则返回原字符串的副本。

'python'.ljust(1)
'python'
'python'.ljust(10,'~')
'python~~~~'
'python'.ljust(10)
'python    '

str.center 居中

字符串方法 str.center(),Python 官方文档描述如下:

help(str.center)
Help on method_descriptor:

center(self, width, fillchar=' ', /)
    Return a centered string of length width.
    
    Padding is done using the specified fill character (default is a space).

返回长度为 width 的字符串,原字符串在其正中。使用指定的 fillchar 填充两边的空位(默认使用ASCII 空格符)。如果 width 小于等于字符串长度,则返回原字符串的副本:

'Python'.center(1)
'Python'
'Python'.center(10)
'  Python  '
'Python'.center(20,'~')
'~~~~~~~Python~~~~~~~'

str.rjust 右对齐

字符串方法 str.rjust(),Python 官方文档描述如下:

help(str.rjust)
Help on method_descriptor:

rjust(self, width, fillchar=' ', /)
    Return a right-justified string of length width.
    
    Padding is done using the specified fill character (default is a space).

返回长度为 width 的字符串,原字符串在其中靠右对齐。使用指定的 fillchar 填充空位 (默认使用 ASCII 空格符)。如果 width 小于等于字符串长度 len(str) 则返回原字符串的副本。

'python'.rjust(1)
'python'
'python'.rjust(10,'~')
'~~~~python'
'python'.rjust(10)
'    python'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值