ADVERTISEMENT
ADVERTISEMENT

Python TextCalendar Class | formatyear() Method with Example

Python TextCalendar.formatyear() Method: Here, we are going to learn about the formatyear() method of TextCalendar class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 09, 2020

Python TextCalendar.formatyear() Method

formatyear() method is an inbuilt method of the TextCalendar class of calendar module in Python. It works on text calendars. It uses an instance of TextCalendar class and returns a multi-line string representation of an m-column calendar of the given year.

Module:

    import calendar

Class:

    from calendar import TextCalendar

Syntax:

    formatyear(year, w=2, l=1, c=6, m=3)

Parameter(s):

  • year: It is a required parameter, which specifies the year of the calendar.
  • w: It is an optional parameter, which specifies the width of the date column; default value = 2.
  • l: It is an optional parameter, which represents the number of lines one week would use in the resulting string; default value = 1.
  • c: It is an optional parameter, which specifies the number of spaces between month columns; default value = 6.
  • m: It is an optional parameter, which represents the number of column of months in a row in the calendar; default value = 3.

Return value:

The return type of this method is <class 'str'>, it returns an m-column calendar of the given year.

Example:

# Python program to illustrate the 
# use of formatyear() method

# import class
import calendar

# creating a TextCalendar instance
cal = calendar.TextCalendar()
year = 2019
print("Printing 2019 Calendar with default parameters w=2, l=1, c=6, m=3")
print(cal.formatyear(year))
print()

# Setting width=2 ,length =2
cal = calendar.TextCalendar()
year = 1919
print("Printing 1919 Calendar with  w=2, l=2")
# parameters missing take their default values
print(cal.formatyear(year, 2, 2))
print()

# Setting width=2 , column gap c=3
cal = calendar.TextCalendar()
year = 1999
print("Printing 1999 Calendar with w=2, l=1, c=3")
print(cal.formatyear(year, 2, 1, 3))
print()

# Changing the firstweekday
cal = calendar.TextCalendar(firstweekday = 3)
# First column will be Thursday
year = 2005
print("Printing 2005 Calendar with default parameters w=2, l=1, c=6, m=3 and first left column starting with Thursday")
print(cal.formatyear(year))
print()

Output

Printing 2019 Calendar with default parameters w=2, l=1, c=6, m=3
                                  2019

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

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


Printing 1919 Calendar with  w=2, l=2
                                  1919



      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



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

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



Printing 1999 Calendar with w=2, l=1, c=3
                               1999

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

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


Printing 2005 Calendar with default parameters w=2, l=1, c=6, m=3 and first left column starting with Thursday
                                  2005

      January                   February                   March
Th Fr Sa Su Mo Tu We      Th Fr Sa Su Mo Tu We      Th Fr Sa Su Mo Tu We
       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
Th Fr Sa Su Mo Tu We      Th Fr Sa Su Mo Tu We      Th Fr Sa Su Mo Tu We
    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
Th Fr Sa Su Mo Tu We      Th Fr Sa Su Mo Tu We      Th Fr Sa Su Mo Tu We
    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
Th Fr Sa Su Mo Tu We      Th Fr Sa Su Mo Tu We      Th Fr Sa Su Mo Tu We
       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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.