当秒小于或等于60秒时,显示时间为1位数的格式,可以通过以下方法实现:
seconds = 45
time_str = f"{seconds:02d}" if seconds > 9 else f"{seconds:d}"
print(time_str) # 输出: 45
str.format()
方法:seconds = 7
time_str = "{:02d}".format(seconds) if seconds > 9 else str(seconds)
print(time_str) # 输出: 7
zfill()
方法:seconds = 30
time_str = str(seconds).zfill(2) if seconds > 9 else str(seconds)
print(time_str) - 输出: 30
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云