zoukankan      html  css  js  c++  java
  • ljust,rjust,center,zfill对齐使用方法

    字符串在输出时的对齐:

    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 >>>
  • 相关阅读:
    数据结构与算法4—队列
    栈的应用——括号匹配
    迷宫求解
    python的socket编程
    数据结构与算法3—栈
    数据结构与算法2—链表
    数据结构与算法1—线性表
    增量解析
    ElementTree类
    节点序列化
  • 原文地址:https://www.cnblogs.com/Alone-Tree/p/10096187.html
Copyright © 2011-2022 走看看