Python Program for calendar prmonth() 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.

prmonth() Method:

The prmonth() method is used to print the month’s calendar.

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

Syntax:

prmonth(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. The distance between two columns in width. The default value is zero.

lines: This is Optional. It is a number. A blank line separating two rows. The default value is zero.

Return Value: A month’s calendar is returned.

Program for calendar prmonth() 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 prmonth() 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 = 2017
# Give the month as static input and store it in another variable.
gvn_mont = 6
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply prmonth() method to the above text calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = txt_calndr.prmonth(gvn_yr, gvn_mont)
# Print the above result.
print(rslt)

Output:

     June 2017
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
None

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 prmonth() 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 = 2012
# Give the month as static input and store it in another variable.
gvn_mont = 9
# Give the width as static input and store it in another variable.
gvn_widt = 4
# Give the no of lines as static input and store it in another variable.
gvn_lines = 2
# Call the TextCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.TextCalendar(firstweekday=0)
# Apply prmonth() 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.prmonth(gvn_yr, gvn_mont, gvn_widt, gvn_lines)
# Print the above result.
print(rslt)

Output:

          September 2012

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

None

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 prmonth() 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 prmonth() method to the above text calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = txt_calndr.prmonth(gvn_yr, gvn_mont)
# Print the above result.
print(rslt)

Output:

Enter some random year = 2015
Enter some random month = 5
May 2015
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
None

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 prmonth() 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 prmonth() 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.prmonth(gvn_yr, gvn_mont, gvn_widt, gvn_lines)
# Print the above result.
print(rslt)

Output:

Enter some random year = 2017
Enter some random month = 6
Enter some random number = 5
Enter some random number = 3
June 2017


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


None