前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python ljust,rjust,center,zfill对齐使用方法

python ljust,rjust,center,zfill对齐使用方法

作者头像
程序员同行者
发布2018-07-02 16:43:41
9200
发布2018-07-02 16:43:41
举报
文章被收录于专栏:程序员同行者

字符串在输出时的对齐: S.ljust(width,[fillchar])  #输出width个字符,S左对齐,不足部分用fillchar填充,默认的为空格。  S.rjust(width,[fillchar]) #右对齐  S.center(width, [fillchar]) #中间对齐  S.zfill(width) #把S变成width长,并在右对齐,不足部分用0补足 

实例

代码语言:javascript
复制
 1 >>> str = "this is string example....wow!!!";
 2 >>> str.ljust(50,'0')
 3 'this is string example....wow!!!000000000000000000'
 4 >>> str.ljust(50)
 5 'this is string example....wow!!!                  '
 6 >>> str.rjust(50)
 7 '                  this is string example....wow!!!'
 8 >>> str.rjust(50,'0')
 9 '000000000000000000this is string example....wow!!!'
10 >>> str.center(50,'0')
11 '000000000this is string example....wow!!!000000000'
12 >>> str.center(50)
13 '         this is string example....wow!!!         '
14 >>> str.zfill(50)
15 '000000000000000000this is string example....wow!!!'
16 >>>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-12-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档