How to pad zeroes to a string?

前端 未结 17 2255
醉酒成梦
醉酒成梦 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:43

    I am adding how to use a int from a length of a string within an f-string because it didn't appear to be covered:

    >>> pad_number = len("this_string")
    11
    >>> s = f"{1:0{pad_number}}" }
    >>> s
    '00000000001'
    
    

提交回复
热议问题