Display number with leading zeros

后端 未结 16 2339
我在风中等你
我在风中等你 2020-11-22 03:00

Given:

a = 1
b = 10
c = 100

How do I display a leading zero for all numbers with less than two digits?

This is the output I\'m expe

16条回答
  •  再見小時候
    2020-11-22 03:21

    You can do this with f strings.

    import numpy as np
    
    print(f'{np.random.choice([1, 124, 13566]):0>8}')
    

    This will print constant length of 8, and pad the rest with leading 0.

    00000001
    00000124
    00013566
    

提交回复
热议问题