用友善的格式列印日期
因為人們可能需要用許多不同的格式來查看日期,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)