首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

当秒小于或等于60秒时,显示时间为1零

当秒小于或等于60秒时,显示时间为1位数的格式,可以通过以下方法实现:

  1. 使用条件表达式:
代码语言:javascript
复制
seconds = 45
time_str = f"{seconds:02d}" if seconds > 9 else f"{seconds:d}"
print(time_str)  # 输出: 45
  1. 使用str.format()方法:
代码语言:javascript
复制
seconds = 7
time_str = "{:02d}".format(seconds) if seconds > 9 else str(seconds)
print(time_str)  # 输出: 7
  1. 使用zfill()方法:
代码语言:javascript
复制
seconds = 30
time_str = str(seconds).zfill(2) if seconds > 9 else str(seconds)
print(time_str)  - 输出: 30
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的合辑

领券