Python calendar month() Method with Example

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

Python calendar.month() Method

The calendar.month() method is an inbuilt method of the calendar module, it returns a multi-line string representation of the given month's calendar.

Module

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

import calendar

Syntax

The following is the syntax of month() method:

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

Parameter(s)

The following are the parameter(s):

  • year: It is a required parameter, which represents the year of the calendar.
  • month: It is a required parameter, which represents the month of the calendar.
  • w: It is an optional parameter, which specifies the width of the date columns, which are centered.
  • l: It is an optional argument, which represents the number of lines each week in the calendar will use.

Return Value

The return type of this method is <class 'str'> (a multi-line string). The method returns the calendar of the given year's month.

Example of calendar.month() Method in Python

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

# importing calendar module
import calendar

# Printing May 2020 with column width=0 
# and number of lines for each week=0
print("Printing May 2020 Calendar with default parameters")
print(calendar.month(2020, 5))
print()

print("Printing October 1980 Calendar with column width=4")
print(calendar.month(1980, 10, 4))
print()

print("Printing December 1999 Calendar with column width=5 and number of lines for each week=2")
print(calendar.month(1999, 12, 5, 2))
print()

calendar.setfirstweekday(4)
# First column on the left will be Friday
# Printing Jaunary 1950
print("Printing January 1950 with column width =5, lines per week = 2 and first column on the left as Friday")
print(calendar.month(1950, 1, 5, 2))

Output

Printing May 2020 Calendar with default parameters
      May 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


Printing October 1980 Calendar with column width=4
           October 1980
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


Printing December 1999 Calendar with column width=5 and number of lines for each week=2
              December 1999

 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



Printing January 1950 with column width =5, lines per week = 2 and first column on the left as Friday
               January 1950

 Fri   Sat   Sun   Mon   Tue   Wed   Thu

               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




Comments and Discussions!

Load comments ↻






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