Python中0补齐的方法

①纯数字补齐:    %换位符 

        例如: 短信验证码

import random
sms_code = "%06d" % random.randint(0, 999999)   # 0是用0补齐, 6是默认位数, d代表是整数
print(sms_code)

②字符串补齐:  内置的zfill方法:

   (参考: http://www.sharejs.com/codes/python/8037)

# 订单号补齐
pre_str = '11012019'
order_no = '1'
order_id = pre_str + order_no.zfill(4)      # 4是补齐后的长度, 默认使用0补齐
print(order_id)

猜你喜欢

转载自blog.csdn.net/qq_42327755/article/details/87189009