【发布时间】:2018-11-11 05:48:26
【问题描述】:
date = [2, 5, 2018]
text = "%s/%s/%s" % tuple(date)
print(text)
它给出了结果2/5/2018。如何像02/05/2018一样转换它
【问题讨论】:
标签: python python-2.7
date = [2, 5, 2018]
text = "%s/%s/%s" % tuple(date)
print(text)
它给出了结果2/5/2018。如何像02/05/2018一样转换它
【问题讨论】:
标签: python python-2.7
text = "{:02d}/{:02d}/{:d}".format(*date)
【讨论】:
使用 str.zfill(2) 用 2 个前导零填充您的日期片段:
date = [2, 5, 2018]
text = "%s/%s/%s" % tuple([str(date_part).zfill(2) for date_part in date])
print(text) # Outputs: 02/05/2018
【讨论】:
date = [2, 5, 2018]
text = "{:0>2}/{:0>2}/{}".format(*date)
print(text)
要了解有关使用 format 的更多信息,请阅读:https://pyformat.info/
【讨论】: