Python calendar.calendar() Function
The Python calendar.calendar() function is used to generate a multi-line string representation of a full calendar for a given year.
This function is useful for displaying or printing an entire year's calendar in a structured format.
Syntax
Following is the syntax of the Python calendar.calendar() function −
calendar.calendar(year, w=2, l=1, c=6, m=3)
Parameters
This function accepts the following parameters −
- year: The year for which the calendar is to be generated.
- w (optional): The width of each date column (default is 2).
- l (optional): The number of lines per week (default is 1).
- c (optional): The space between month columns (default is 6).
- m (optional): The number of months per row (default is 3).
Return Value
This function returns a multi-line string containing the formatted calendar for the specified year.
Example: Generating a Yearly Calendar
In this example, we use the calendar.calendar() function to generate a full-year calendar for 2025 −
import calendar # Generate calendar for the year 2025 year_calendar = calendar.calendar(2025) print(year_calendar)
Following is an excerpt of the output obtained −
2025
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
...
Example: Customizing Calendar Layout
We can adjust the width of date columns, number of months per row, and spacing using optional parameters −
import calendar # Generate calendar with custom formatting custom_calendar = calendar.calendar(2025, w=3, l=1, c=10, m=4) print(custom_calendar)
Following is an excerpt of the customized calendar output −
2025
January February March April
Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5 1 2 1 2 1 2 3 4 5 6
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30 28 29 30
31
...
Example: Calendar with Increased Line Spacing
By modifying the l parameter, we can increase the number of lines per week for better readability −
import calendar # Generate calendar with increased line spacing spaced_calendar = calendar.calendar(2025, l=2) print(spaced_calendar)
Following is an excerpt of the spaced calendar output −
2025
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
...
Example: Compact Calendar with Fewer Columns
Reducing the m parameter to 2 allows displaying only two months per row, making the calendar more compact −
import calendar # Generate a more compact calendar layout compact_calendar = calendar.calendar(2025, m=2) print(compact_calendar)
Following is an excerpt of the compact calendar output −
2025
January February
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28
March April
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 1 2 3 4 5 6
3 4 5 6 7 8 9 7 8 9 10 11 12 13
10 11 12 13 14 15 16 14 15 16 17 18 19 20
17 18 19 20 21 22 23 21 22 23 24 25 26 27
24 25 26 27 28 29 30 28 29 30
31
...
python_date_time.htm