ADVERTISEMENT
ADVERTISEMENT

Python TextCalendar Class | formatmonth() Method with Example

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

Python TextCalendar.formatmonth() Method

formatmonth() method is an inbuilt method of the TextCalendar class of calendar module in Python. It works on text calendars and returns a multi-line string representing the calendar of the given month.

Module:

    import calendar

Class:

    from calendar import TextCalendar

Syntax:

    formatmonth(year, month, w=0, l=0)

Parameter(s):

  • year: It is a required parameter, which specifies the year of the calendar.
  • month: It is a required parameter, which specifies the month of the calendar.
  • w: It is an optional parameter, which specifies the width of the date column; default value = 0.
  • l: It is an optional parameter, which represents the number of lines one week would use in the resulting string; default value = 0.

Return value:

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

Example:

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

# import class
import calendar

# creating a TextCalendar instance
cal = calendar.TextCalendar()
year = 2019
month = 12
# default width =0
print("Month's calendar:", cal.formatmonth(year, month))
print()

# varying width and length
cal = calendar.TextCalendar()
year = 1996
month = 2
#  width=5, length=2
print("Month's calendar:", cal.formatmonth(year, month, 5, 2))
print()

# changing the firstweekday() for a different display
cal = calendar.TextCalendar(firstweekday=3)
# This will display the first column as Thursday
year = 1819
month = 9
# width=3
print("Month's calendar:", cal.formatmonth(year, month, 3))
print()

Output

Month's calendar:    December 2019
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


Month's calendar:               February 1996

 Mon   Tue   Wed   Thu   Fri   Sat   Sun

                     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



Month's calendar:        September 1819
Thu Fri Sat Sun Mon Tue Wed
                          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
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.