ADVERTISEMENT
ADVERTISEMENT

Python calendar Module | month() Method with Example

Python calendar.month() Method: Here, we are going to learn about the month() method of calendar module in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 08, 2020

Python calendar.month() Method

month() method is an inbuilt method of the calendar module in Python. It works on simple text calendars and returns a multi-line string representation of the given month's calendar using the formatmonth() of the TextCalendar class. It is similar to prmonth() function except it returns a string where prmonth() method does not return anything.

Module:

    import calendar

Syntax:

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

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:

# 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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.