How to display 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.

How to display calendar in python

Updated on 12-Feb-2020 12:21:27

  • Related Questions & Answers
  • How to print calendar for a month in Python?
  • Print calendar for a given year in C++
  • Calendar in python
  • Calendar Functions in Python - ( calendar(), month(), isleap()?)
  • How to print a calendar for the given month and year in Python?
  • Calendar function in Python
  • Calendar in Python Program
  • Getting calendar for a month in Python
  • Calendar Functions in Python | Set 1( calendar(), month(), isleap()…)
  • Explain calendar module in python?
  • What is calendar module in python?
  • Calendar Functions in Python -(monthrange(), prcal(), weekday()?)
  • The calendar Module in Python
  • How to Display Calendar using Python?
  • How to print a one-month calendar of user choice using for loop in C?

How do I show a calendar in Python?

It is simple in python programming to display calendar. To do so, you need to import the calendar module which comes with Python..
import calendar..
# Enter the month and year..
yy = int(input("Enter year: ")).
mm = int(input("Enter month: ")).
# display the calendar..
print(calendar. month(yy,mm)).

What is the command used to print calendar in Python?

Import the calendar module. Initialize the year number. Print the calendar using calendar. calendar(year) class.

How do I print a whole year calendar in Python?

Source Code: import calendar //import the calendar module def printcalendar(yr): # printing calendar print(calendar. calendar(yr)) # driver program yr = 2019 printcalendar(yr) Output: The whole calendar year of 2019 will be displayed as the output.

What does month () do in Python?

The month() method is used to get a month's calendar in a multi-line string using the formatmonth() of the TextCalendar class. Year for which the calendar should be generated. Month for which the calendar should be generated.