86.4k views
0 votes
The datetime module provides date and time objects that are similar to the Date and Time objects in this chapter, but they provide a rich set of methods and operators. Read the documentation at docs. python. org/lib/ datetime-date. html.

(1) Use the datetime module to write a program that gets the current date and prints the day of the week.

1 Answer

4 votes

Final answer:

To print the current day of the week in Python, use the datetime module's datetime.now() method and format the output with strftime("%A").

Step-by-step explanation:

To get the current date and print the day of the week in Python, you can use the datetime module. This module offers various classes for manipulating dates and times, and is quite easy to use for this purpose. Here's a simple program to accomplish what you're asking:

from datetime import datetime

today = datetime.now()
print(today.strftime("%A"))

This code imports the datetime class from the datetime module. We then get the current date and time using datetime.now(), and strftime("%A") is used to format the date object to display the full weekday name.

User Isaac Gregson
by
8.8k points