How to pad zeroes to a string?

前端 未结 17 1947
醉酒成梦
醉酒成梦 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 23:29

    Just use the rjust method of the string object.

    This example will make a string of 10 characters long, padding as necessary.

    >>> t = 'test'
    >>> t.rjust(10, '0')
    >>> '000000test'
    

提交回复
热议问题