python将221变为‘020201’
可以使用字符串的format方法来实现:
time_str = '2:2:1'
time_str = ':'.join([x.zfill(2) for x in time_str.split(':')])
print(time_str) # 输出 '02:02:01'
首先,使用split方法将字符串分割为一个列表,然后使用zfill方法给每个元素填充0,再使用join方法将列表中的元素用冒号连接起来,最后得到'02:02:01'的结果。
原文地址: https://www.cveoy.top/t/topic/iq47 著作权归作者所有。请勿转载和采集!