代码之家  ›  专栏  ›  技术社区  ›  deworde

Python:如何将一个int转换成一组数字的字符串表示形式?

  •  1
  • deworde  · 技术社区  · 14 年前

    和上面说的差不多。

    基本上,我如何得到

    print "%05d" % 100
    
    6 回复  |  直到 14 年前
        1
  •  11
  •   Bruno    14 年前

    也许我误解了这个问题,但这应该管用:

    my_string = "%05d" % 100
    
        2
  •  1
  •   deworde    14 年前

    使用街道zfill(宽度)

        3
  •  1
  •   Tarantula    14 年前

    `100`.zfill(5)
    
        4
  •  1
  •   unutbu    14 年前
    print('{0:0=5d}'.format(100))
    # 00100
    
    
        use the 0th positional argument to format
       /  fill character is '0'
      /  / desired width of formatted string
     /  / /
    {0:0=5d}
    

    有关详细信息,请参阅 the docs

        5
  •  1
  •   FlowofSoul    14 年前
    i = 100
    str(i).zfill(5)
    
        6
  •  0
  •   GreenMatt    14 年前

    如果您使用的是python3 str.format