Editor

Python Program to Print a Calendar

Beginner · Dates and times

This program prints the calendar of a month, laid out in weeks like a wall calendar, using Python's built-in calendar module. It also shows how many days the month has and which day of the week it starts on.

Program

print_calendar.py
import calendar
year = 2026 # or: year = int(input("Enter the year: "))
month = 10 # or: month = int(input("Enter the month (1-12): "))
print(calendar.month(year, month))
first_day, num_days = calendar.monthrange(year, month)
print(calendar.month_name[month], year, "has", num_days, "days")
print("It starts on a", calendar.day_name[first_day])
print("Is", year, "a leap year?", calendar.isleap(year))
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

October 2026 has 31 days
It starts on a Thursday
Is 2026 a leap year? False

How it works

  1. calendar.month(year, month) returns the whole month as a block of text, with the days of the week as columns starting from Monday.
  2. calendar.monthrange(year, month) returns two numbers: the weekday of the 1st (0 for Monday) and how many days the month has.
  3. calendar.month_name and calendar.day_name turn those numbers into names such as October and Thursday.
  4. calendar.isleap(year) tells you whether February has 29 days that year.

Other ways to write it

Weeks starting on Sunday

Weeks starting on Sunday
import calendar
cal = calendar.TextCalendar(calendar.SUNDAY) # Sunday as the first column
print(cal.formatmonth(2026, 10))
Output
    October 2026
Su Mo Tu We Th Fr Sa
             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

The default calendar starts each week on Monday; TextCalendar(calendar.SUNDAY) starts it on Sunday, like most calendars printed in India.

Drawing the month yourself

Drawing the month yourself
import calendar
year, month = 2026, 10
first_day, num_days = calendar.monthrange(year, month) # Monday is 0
print("Mo Tu We Th Fr Sa Su")
print(" " * first_day, end="") # blank slots before the 1st
for day in range(1, num_days + 1):
print(f"{day:2}", end=" ")
if (first_day + day) % 7 == 0: # a Sunday ends the row
print()
print()
Output
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

This is roughly what the calendar module does: leave a blank slot for each weekday before the 1st, then print the days and start a new row after every Sunday.

Try it yourself

Print the calendar for the whole year with print(calendar.calendar(2026)).

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises