How to pad zeroes to a string?

前端 未结 17 1982
醉酒成梦
醉酒成梦 2020-11-21 22:59

What is a Pythonic way to pad a numeric string with zeroes to the left, i.e. so the numeric string has a specific length?

17条回答
  •  臣服心动
    2020-11-21 23:32

    >>> '99'.zfill(5)
    '00099'
    >>> '99'.rjust(5,'0')
    '00099'
    

    if you want the opposite:

    >>> '99'.ljust(5,'0')
    '99000'
    

提交回复
热议问题