首页 > 编程语言 > 详细

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

时间:2016-12-05 11:35:33      阅读:664      评论:0      收藏:0      [点我收藏+]

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

实例

 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 >>>

 

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

原文:http://www.cnblogs.com/guigujun/p/6133279.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!