python 数字字符串获取固定长度的数值,不够的左边补0

Python zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0

语法格式:str.zfill(width),width指定字符串的长度 

stk_code = 1
     
stk_code  = str(stk_code).zfill(6)
     
print(stk_code )
     
输出结果:000001

 

posted @ 2022-01-25 18:05  树下水月  阅读(435)  评论(0编辑  收藏  举报