Python Program for calendar formatmonth() Method with Examples

Calendar Module:

The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.

formatmonth() Method:

To obtain a month’s calendar in a multi-line string, use the formatmonth() method.

To generate plain text calendars, class calendar.TextCalendar(firstweekday=0) can be used.The formatmonth() method is one of the TextCalendar instance’s methods.

Syntax:

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

Parameter Values:

year: This is required. It is a number. It is the year for which the calendar should be created.

month: This is required. It is a number. It is the month for which the calendar should be created.

width: This is Optional. It is a number. It Specifies the width of the centered date columns. The default value is zero.

lines: This is Optional. It is a number. It Specifies the number of lines that each week will use. The default value is zero.

Return Value: It returns a calendar for the month.

Program for calendar formatmonth() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Give the month as static input and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatmonth() method to the above text calendar by passing the given year, month as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2012
# Give the month as static input and store it in another variable.
gvn_mont = 8
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply formatmonth() method to the above text calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = txt_calndr.formatmonth(gvn_yr, gvn_mont)
# Print the above result.
print(rslt)

Output:

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

Example2:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Give the month as static input and store it in another variable.
  • Give the width as static input and store it in another variable.
  • Give the no of lines as static input and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatmonth() method to the above text calendar by passing the given year, month, width, no of lines as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2019
# Give the month as static input and store it in another variable.
gvn_mont = 11
# Give the width as static input and store it in another variable.
gvn_widt = 5
# Give the no of lines as static input and store it in another variable.
gvn_lines = 3
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply formatmonth() method to the above text calendar by passing the given year,
# month, width, no of lines as the arguments and store it in another variable.
rslt = txt_calndr.formatmonth(gvn_yr, gvn_mont, gvn_widt, gvn_lines)
# Print the above result.
print(rslt)

Output:

              November 2019


 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

Method #2: Using Built-in Functions (User Input)

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Give the month as user input using the int(input()) function and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatmonth() method to the above text calendar by passing the given year, month as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr = int(input("Enter some random year = "))
# Give the month as user input using the int(input()) function and store it in another variable.
gvn_mont = int(input("Enter some random month = "))
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply formatmonth() method to the above text calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = txt_calndr.formatmonth(gvn_yr, gvn_mont)
# Print the above result.
print(rslt)

Output:

Enter some random year = 2020
Enter some random month = 3
March 2020
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

Example2:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Give the month as user input using the int(input()) function and store it in another variable.
  • Give the width as user input using the int(input()) function and store it in another variable.
  • Give the no of lines as user input using the int(input()) function and store it in another variable.
  • Call the TextCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatmonth() method to the above text calendar by passing the given year, month, width, no of lines as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr = int(input("Enter some random year = "))
# Give the month as user input using the int(input()) function and store it in another variable.
gvn_mont = int(input("Enter some random month = "))
# Give the width as user input using the int(input()) and store it in another variable.
gvn_widt = int(input("Enter some random number = "))
# Give the no of lines user input using the int(input()) and store it in another variable.
gvn_lines = int(input("Enter some random number = "))
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply formatmonth() method to the above text calendar by passing the given year,
# month, width, no of lines as the arguments and store it in another variable.
rslt = txt_calndr.formatmonth(gvn_yr, gvn_mont, gvn_widt, gvn_lines)
# Print the above result.
print(rslt)

Output:

Enter some random year = 2021
Enter some random month = 5
Enter some random number = 2
Enter some random number = 2
May 2021

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