How to pad a numeric string with zeros to the right in Python?

后端 未结 3 1225
借酒劲吻你
借酒劲吻你 2020-12-30 23:34

Python strings have a method called zfill that allows to pad a numeric string with zeros to the left.

In : str(190).zfill(8)
Out: \'00000190\'
<         


        
3条回答
  •  悲&欢浪女
    2020-12-31 00:30

    See Format Specification Mini-Language:

    In [1]: '{:<08d}'.format(190)
    Out[1]: '19000000'
    
    In [2]: '{:>08d}'.format(190)
    Out[2]: '00000190'
    

提交回复
热议问题