Python TextCalendar formatmonth() Method with Example

Python TextCalendar.formatmonth() Method: In this tutorial, we will learn about the formatmonth() method of TextCalendar class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 24, 2023

Python TextCalendar.formatmonth() Method

The Text.Calendar.formatmonth() method is an inbuilt method of the TextCalendar class of calendar module, it is used to get a multi-line string representing the calendar of the given month.

Module

The following module is required to use formatmonth() method:

import calendar

Class

The following class is required to use formatmonth() method:

from calendar import TextCalendar

Syntax

The following is the syntax of formatmonth() method:

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

Parameter(s)

The following are the 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 of TextCalendar.formatmonth() Method in Python

# 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



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.