The Wayback Machine - https://web.archive.org/web/20240524235948/https://www.geeksforgeeks.org/python-calendar-module-formatmonth-method/
Open In App

Python calendar module : formatmonth() method

Last Updated : 04 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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. formatmonth() method is one of the methods of TextCalendar instance.

formatmonth() method in Python is used to get month’s calendar in a multi-line string.

Syntax: formatmonth(year, month, width=0, lines=0)

Parameter:
year: year of the calendar
month: month of the calendar
width: [optional] Specifies the width of the date columns, which are centered
line: [optional] Specifies the number of lines that each week will use.

Returns: Return a month’s calendar.

Code #1:




# Python program to demonstrate working of formatmonth() method
  
# importing calendar module
import calendar
  
text_cal = calendar.TextCalendar(firstweekday = 0)
  
year = 2018
month = 9
# default value of width is 0
  
# printing formatmonth
print(text_cal.formatmonth(year, month))


Output:

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

 

Code #2: With parameter width




# Python program to demonstrate working of formatmonth() method
  
# importing calendar module
import calendar
  
text_cal = calendar.TextCalendar(firstweekday = 0)
  
  
# default value of width is 0
  
# printing formatmonth
print(text_cal.formatmonth(2018, 9, w = 5))


Output:

              September 2018
 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    30

 
Code #3:




# Python program to demonstrate working of formatmonth() method
  
# importing calendar module
import calendar
  
text_cal = calendar.TextCalendar(firstweekday = 0)
  
  
# giving value of width = 6, line = 2
  
# printing formatmonth
print(text_cal.formatmonth(2018, 10, 6, 2))


Output:

                  October 2018

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


Previous Article
Next Article

Similar Reads

Python calendar module | HTMLCalendar formatmonth() 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.HTMLCalendar(firstweekday=0) can be used to generate HTML calen
4 min read
Python program to print Calendar without calendar or datetime module
Given the month and year. The task is to show the calendar of that month and in the given year without using any module or pre-defined functions. Examples: Input : mm(1-12) :9 yy :2010 Output : September 2010 Su Mo Tu We Th Fr Sa 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Approach: In the program below
3 min read
Calendar Functions in Python | Set 1( calendar(), month(), isleap()...)
Python defines an inbuilt module "calendar" which handles operations related to the calendar.Operations on the calendar : 1. calendar(year, w, l, c):- This function displays the year, the width of characters, no. of lines per week, and column separations.2. firstweekday() :- This function returns the first week day number. By default 0 (Monday). C/
5 min read
Python calendar module | itermonthdays2() 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. itermonthdays2() method is used to get an iterator for the month in the year s
2 min read
Python calendar module : iterweekdays() 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. iterweekdays() method returns an iterator for the week day numbers that will b
1 min read
Python calendar module : itermonthdays() 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. itermonthdays() method returns an iterator of a specified month and a year. Da
1 min read
Python calendar module : monthdatescalendar() 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. monthdatescalendar() method in Python is used to get a list of the weeks in th
2 min read
Python calendar module : monthdays2calendar() 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. monthdays2calendar() method in Python is used to get a list of the weeks in th
2 min read
Python calendar module | monthdayscalendar() 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. monthdayscalendar() method in Python is used to get a list of the weeks in the
2 min read
Python calendar module : yeardatescalendar() 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. yeardatescalendar() method in Python is used to get a list of the weeks in the
3 min read
Practice Tags :