python字符串居中对齐_Python center()字符串居中对齐方法详解

center() 字符串方法与ljust()和rjust()的用法类似,唯一的不同在于,该方法的功能是让字符串居中,而不是左对齐或右对齐。

center() 方法的基本格式如下:

S.center(width[, fillchar])

其中各个参数的含义如下:

S:表示要进行填充的字符串;

width:表示包括 S 本身长度在内,字符串要占的总长度;

fillchar:作为可选参数,用来指定填充字符串时所用的字符,默认情况使用空格。

【例 1】

S = 'http://c.biancheng.net/python/'

addr = 'http://c.biancheng.net'

print(S.center(35,))

print(addr.center(35,))

输出结果为:

http://c.biancheng.net/python/

http://c.biancheng.net

【例 2】

S = 'http://c.biancheng.net/python/'

addr = 'http://c.biancheng.net'

print(S.center(35,'-'))

print(addr.center(35,'-'))

输出结果为:

---http://c.biancheng.net/python/--

-------http://c.biancheng.net------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值