用不同方式表示日期
在 Python 中,date 对象可以通过多种方式打印为字符串。有时,您需要一种清晰、与语言无关的格式来表示日期。另一些情况下,您需要让日期自然融入段落的表述。
我们来练习使用 .strftime() 方法,把同一个日期(1992年8月26日,飓风 Andrew 登陆佛罗里达的那天)以多种不同的方式打印出来。
名为 andrew 的日期对象已经创建好。
本练习是课程的一部分
在 Python 中处理日期和时间
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import date
from datetime import date
# Create a date object
andrew = date(1992, 8, 26)
# Print the date in the format 'YYYY-MM'
print(andrew.strftime(____))