Saturday, December 28, 2024
Google search engine
HomeLanguagesPython calendar module : formatyear() method

Python calendar module : formatyear() method

Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions.

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

formatyear() method in Python is used to get m-column calendar for an entire year as a multi-line string.

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

Parameter:
year: year of the calendar
width: [optional] Specifies the width date column
line: [optional] Specifies the number of lines that each week will use.
c: [optional] Number of spaces between month columns
m: [optional] Number of months in a row

Returns: Return a m-column calendar for an entire year.

Depends on the first weekday as specified in the constructor or set by the setfirstweekday() method.

Code #1:




# Python program to demonstrate working of formatyear() method
  
# importing calendar module
import calendar
  
text_cal = calendar.TextCalendar(firstweekday = 0)
  
year = 2018
width = 4
  
# printing formatyear
print(text_cal.formatyear(year, width))


Output:


                                                       2018

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

Note that the earliest year for which a calendar can be generated is platform-dependent.
 

Code #2: Since m=2, the number of month shown in a row will be 2.




# Python program to demonstrate working of formatyear() method
  
# importing calendar module
import calendar
  
text_cal = calendar.TextCalendar(firstweekday = 0)
  
  
# default value of width is 0
  
# printing formatyear
print(text_cal.formatyear(2018, 5, c = 3, m = 2))


Output:

                                        2018

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

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

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

                   July                                       August
 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    31
  30    31

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

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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments