Python calendar.prmonth() Function



The Python calendar.prmonth() function prints the calendar for a specific month of a given year in a formatted manner.

This function is useful for displaying a monthly calendar directly to the console.

Syntax

Following is the syntax of the Python calendar.prmonth() function −

calendar.prmonth(year, month, w=0, l=0)

Parameters

This function accepts the following parameters −

  • year: The year of the month to be displayed.
  • month: The month (1-12) to be displayed.
  • w (optional): Width of each date column (default is 0).
  • l (optional): Number of lines per week (default is 0).

Return Value

This function does not return a value. It prints the formatted monthly calendar directly to the console.

Example: Printing a Calendar for Given Month

In this example, we print the calendar for March 2025 using the calendar.prmonth() function −

import calendar

# Print the calendar for March 2025
calendar.prmonth(2025, 3)

Following is the output obtained −

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

 

 

 

 

 

 

Example: Calendar with Custom Column Width

We can adjust the width of each date column using the w parameter in the calendar.prmonth() function −

import calendar

# Print the calendar for March 2025 with a column width of 4
calendar.prmonth(2025, 3, w=4)

Following is an excerpt of the output obtained −

    March 2025
Mo   Tu   We   Th   Fr   Sa   Su
              1    2
 3    4    5    6    7    8    9
10   11   12   13   14   15   16
...

Example: Calendar with Increased Line Spacing

We can adjust the number of lines per week using the l parameter in the prmonth() function −

import calendar

# Print the calendar for March 2025 with extra line spacing
calendar.prmonth(2025, 3, l=2)

We get the output as shown below −

    March 2025
Mo Tu We Th Fr Sa Su
                  1  2

 3  4  5  6  7  8  9

10 11 12 13 14 15 16
...

Example: Calendar for February 2024 (Leap Year)

Printing the month of February in a leap year demonstrates correct day allocation −

import calendar

# Print the calendar for February 2024 (leap year)
calendar.prmonth(2024, 2)

Following is the output obtained −

   February 2024
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
python_date_time.htm
Advertisements