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 31How it works
calendar.month(year, month)returns the month as one string with line breaks, soprint()shows it as a grid.- Weeks start on Monday by default. For weeks that start on Sunday, call
calendar.setfirstweekday(calendar.SUNDAY)first. - The day and month names follow the computer's language settings, so on some computers they are not in English.
calendar.calendar(2026)returns the whole year in the same way.
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.
calendar.monthrange(year, month)returns two numbers: the weekday of the first day of the month (0 is Monday, 6 is Sunday) and the number of days in the month.[1]takes only the number of days. It knows about leap years, so February 2028 has 29 days. The leap year program shows the rule that decides this.date.weekday()gives the same 0 to 6 number for any date, andcalendar.day_nameturns it into a name.{launch:%A}is a shorter way to get the day name from a date.