以友好格式打印日期
由于人们可能希望以多种不同格式查看日期,Python 提供了非常灵活的函数来将 date 对象转换为字符串。
让我们看看佛罗里达飓风数据集中最早记录的事件。在本练习中,您将把 florida_hurricane_dates 列表中的最早日期用两种方式进行格式化,以便决定使用哪一种:ISO 标准或典型的美国风格。
本练习是课程的一部分
在 Python 中处理日期和时间
练习说明
- 将
florida_hurricane_dates中最早的日期赋给first_date。 - 以 ISO 标准打印
first_date。例如,2000 年 12 月 1 日应为 "2000-12-01"。 - 使用
.strftime()以美国风格打印first_date。例如,2000 年 12 月 1 日应为 "12/1/2000"。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Assign the earliest date to first_date
first_date = ____(florida_hurricane_dates)
# Convert to ISO and US formats
iso = "Our earliest hurricane date: " + first_date.____()
us = "Our earliest hurricane date: " + first_date.____("____")
print("ISO: " + iso)
print("US: " + us)