Python Program for calendar formatyear() Method with Examples

Calendar Module:

The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.

formatyear() Method:

The formatyear() method returns a multi-line string containing an m-column calendar for a full year.

To generate plain text calendars, class calendar.TextCalendar(firstweekday=0) can be used.The formatyear() method is one of the TextCalendar instance’s methods.

Syntax:

formatyear(year, width=2, lines=1, c=6, m=3)

Parameter Values:

year: This is required. It is a number. It is the year for which the calendar should be created.

width: This is Optional. It is a number. The distance between two columns. 2 is the default value.

lines: This is Optional. It is a number. A blank line between two rows. 1 is the default value.

c: This is Optional. It is a number. It is the space between two months (column-wise). 6 is the default value.

m: This is Optional. It is a number. It is the number of months in a row. 3 is the default value.

Return Value: A m-column calendar for the whole year is returned.

Program for calendar formatyear() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Give the width as static input and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatyear() method to the above text calendar by passing the given year, width as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2015
# Give the width as static input and store it in another variable.
gvn_widt = 3
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply formatyear() method to the above text calendar by passing the given year,
# width,as the arguments and store it in another variable.
rslt = txt_calndr.formatyear(gvn_yr, gvn_widt)
# Print the above result.
print(rslt)

Output:

                                             2015

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

           April                             May                              June
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   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
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   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       21  22  23  24  25  26  27
 27  28  29  30  31               24  25  26  27  28  29  30       28  29  30
                                  31

          October                          November                         December
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                                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

Example2:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Give the width as static input and store it in another variable.
  • Give the no of lines as static input and store it in another variable.
  • Give the c value as static input and store it in another variable.
  • Give the m value as static input and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatyear() method to the above text calendar by passing the given year, month, width, no of lines, c, m, values as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2020
# Give the width as static input and store it in another variable.
gvn_widt = 4
# Give the no of lines as static input and store it in another variable.
gvn_lines = 1
# Give the c value as static input and store it in another variable.
gvn_c_val = 3
# Give the m value as static input and store it in another variable.
gvn_m_val = 2
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply formatyear() method to the above text calendar by passing the given year,
# width, lines, c, m values as the arguments and store it in another variable.
rslt = txt_calndr.formatyear(gvn_yr, gvn_widt, gvn_lines, gvn_c_val, gvn_m_val)
# Print the above result.
print(rslt)

Output:

                                  2020

             January                              February
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
            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   29

              March                                April
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
                                1                1    2    3    4    5
  2    3    4    5    6    7    8      6    7    8    9   10   11   12
  9   10   11   12   13   14   15     13   14   15   16   17   18   19
 16   17   18   19   20   21   22     20   21   22   23   24   25   26
 23   24   25   26   27   28   29     27   28   29   30
 30   31

               May                                  June
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
                      1    2    3      1    2    3    4    5    6    7
  4    5    6    7    8    9   10      8    9   10   11   12   13   14
 11   12   13   14   15   16   17     15   16   17   18   19   20   21
 18   19   20   21   22   23   24     22   23   24   25   26   27   28
 25   26   27   28   29   30   31     29   30

               July                                August
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
            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   29   30
                                      31

            September                             October
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
       1    2    3    4    5    6                     1    2    3    4
  7    8    9   10   11   12   13      5    6    7    8    9   10   11
 14   15   16   17   18   19   20     12   13   14   15   16   17   18
 21   22   23   24   25   26   27     19   20   21   22   23   24   25
 28   29   30                         26   27   28   29   30   31

             November                             December
Mon  Tue  Wed  Thu  Fri  Sat  Sun    Mon  Tue  Wed  Thu  Fri  Sat  Sun
                                1           1    2    3    4    5    6
  2    3    4    5    6    7    8      7    8    9   10   11   12   13
  9   10   11   12   13   14   15     14   15   16   17   18   19   20
 16   17   18   19   20   21   22     21   22   23   24   25   26   27
 23   24   25   26   27   28   29     28   29   30   31
 30

Method #2: Using Built-in Functions (User Input)

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Give the width as user input using the int(input()) function and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatyear() method to the above text calendar by passing the given year, width as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr =  int(input("Enter some random year = "))
# Give the width as user input using the int(input()) function and store it in another variable.
gvn_widt =  int(input("Enter some random number = "))
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply formatyear() method to the above text calendar by passing the given year,
# width,as the arguments and store it in another variable.
rslt = txt_calndr.formatyear(gvn_yr, gvn_widt)
# Print the above result.
print(rslt)

Output:

Enter some random year = 2014
Enter some random number = 4
                                                       2014

             January                                 February                                 March
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
  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
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    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

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

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

 

Example2:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Give the width as user input using the int(input()) function and store it in another variable.
  • Give the no of lines as user input using the int(input()) function and store it in another variable.
  • Give the c value as user input using the int(input()) function and store it in another variable.
  • Give the m value as user input using the int(input()) function and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatyear() method to the above text calendar by passing the given year, month, width, no of lines, c, m, values as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr =  int(input("Enter some random year = "))
# Give the width as user input using the int(input()) function and store it in another variable.
gvn_widt =  int(input("Enter some random number = "))
# Give the no of lines as user input using the int(input()) function and store it in another variable.
gvn_lines = int(input("Enter some random number = "))
# Give the c value as user input using the int(input()) function and store it in another variable.
gvn_c_val = int(input("Enter some random number = "))
# Give the m value as user input using the int(input()) function and store it in another variable.
gvn_m_val = int(input("Enter some random number = "))
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply formatyear() method to the above text calendar by passing the given year,
# width, lines, c, m values as the arguments and store it in another variable.
rslt = txt_calndr.formatyear(gvn_yr, gvn_widt, gvn_lines, gvn_c_val, gvn_m_val)
# Print the above result.
print(rslt)

Output:

Enter some random year = 2012
Enter some random number = 3
Enter some random number = 1
Enter some random number = 3
Enter some random number = 3
                                          2012

          January                       February                       March
Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
                          1             1   2   3   4   5                 1   2   3   4
  2   3   4   5   6   7   8     6   7   8   9  10  11  12     5   6   7   8   9  10  11
  9  10  11  12  13  14  15    13  14  15  16  17  18  19    12  13  14  15  16  17  18
 16  17  18  19  20  21  22    20  21  22  23  24  25  26    19  20  21  22  23  24  25
 23  24  25  26  27  28  29    27  28  29                    26  27  28  29  30  31
 30  31

           April                          May                           June
Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
                          1         1   2   3   4   5   6                     1   2   3
  2   3   4   5   6   7   8     7   8   9  10  11  12  13     4   5   6   7   8   9  10
  9  10  11  12  13  14  15    14  15  16  17  18  19  20    11  12  13  14  15  16  17
 16  17  18  19  20  21  22    21  22  23  24  25  26  27    18  19  20  21  22  23  24
 23  24  25  26  27  28  29    28  29  30  31                25  26  27  28  29  30
 30

            July                         August                      September
Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun   Mon Tue Wed Thu Fri Sat Sun
                          1             1   2   3   4   5                         1   2
  2   3   4   5   6   7   8     6   7   8   9  10  11  12     3   4   5   6   7   8   9
  9  10  11  12  13  14  15    13  14  15  16  17  18  19    10  11  12  13  14  15  16
 16  17  18  19  20  21  22    20  21  22  23  24  25  26    17  18  19  20  21  22  23
 23  24  25  26  27  28  29    27  28  29  30  31            24  25  26  27  28  29  30
 30  31

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