PythonMastery

Print a calendar in Python

Print a month as a calendar with calendar.month, find how many days a month has with calendar.monthrange, and find the weekday of any date.

The calendar module in the standard library prints a whole month as text, laid out in weeks. It also answers questions such as "how many days does February have this year?".

The program

python
import calendar

print(calendar.month(2026, 10))
output
    October 2026
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

How it works

Days in a month and the weekday of a date

python
import calendar
from datetime import date

first_weekday, days = calendar.monthrange(2026, 2)
print(f"February 2026 has {days} days and starts on a {calendar.day_name[first_weekday]}.")
print(f"February 2028 has {calendar.monthrange(2028, 2)[1]} days.")

launch = date(2026, 10, 22)
print(f"{launch} is a {calendar.day_name[launch.weekday()]}.")
print(f"{launch} is a {launch:%A}.")
output
February 2026 has 28 days and starts on a Sunday.
February 2028 has 29 days.
2026-10-22 is a Thursday.
2026-10-22 is a Thursday.

Learn the idea: Dates, Times, and Timezones, Modules & Imports