Print a Calendar in Python


In this tutorial, we are going to learn how to print the calendar of month and year using the calendar module of Python. It's a straightforward thing in Python. We need year and month numbers. That's it.

Let's see how to print a year calendar. Follow the below steps to print the year calendar.

  • Import the calendar module.

  • Initialize the year number.

  • Print the calendar using calendar.calendar(year) class.

Example

See the below code.

 Live Demo

# importing the calendar module
import calendar
# initializing the year
year = 2020
# printing the calendar
print(calendar.calendar(year))

Output

If you run the above code, you will get the following output.

                                    2020
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
6  7  8   9 10 11 12              3  4  5  6  7  8  9                           2  3  4  5  6  7  8
13 14 15 16 17 18 19             10 11 12 13 14 15 16                           9 10 11 12 13 14 15
20 21 22 23 24 25 26             17 18 19 20 21 22 23                          16 17 18 19 20 21 22
27 28 29 30 31                   24 25 26 27 28 29                             23 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                          1  2  3                         1  2  3  4  5  6  7
6  7  8  9  10 11 12              4  5  6  7  8  9 10                         8  9 10 11 12 13 14
13 14 15 16 17 18 19             11 12 13 14 15 16 17                        15 16 17 18 19 20 21
20 21 22 23 24 25 26             18 19 20 21 22 23 24                        22 23 24 25 26 27 28
27 28 29 30                      25 26 27 28 29 30 31                        29 30

July                                 August                                     September
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  3   4   5   6
6  7  8  9  10 11 12             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                          14  15  16 17 18  19  20
20 21 22 23 24 25 26            17 18 19 20 21 22 23                          20  21  22 23 24  25  26
27 28 29 30 31                  24 25 26 27 28 29 30                          27  28  29 30
                                31
October                              November                                   December
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                                 1                             1  2  3  4  5  6
5   6  7  8  9  10 11              2  3  4  5  6  7  8                          7  8  9  10 11 12 13
12  13 14 15 16 17 18              9 10  11 12 13 14 15                         14 15 16 17 18 19 20
19  20 21 22 23 24 25              16 17 18 19 20 21 22                         21 22 23 24 25 26 27
26  27 28 29 30 31                 23 24 25 26 27 28 29                         28 29 30 31
                                  30

 Let's see how to print a month calendar. Follow the below steps to print the month calendar.

  • Import the calendar module.

  • Initialize the year and month number.

  • Print the calendar using calendar.month(year, month) class.

Example

See the below code.

 Live Demo

# importing the calendar module
import calendar
# initializing the year and month
year = 2020
month = 1
# printing the calendar
print(calendar.month(year, month))

Output

If you execute the above program, you will get the following result.

January 2020
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

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

Updated on: 12-Feb-2020

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements