What is Python calendar: formatyear(theyear, w=2, l=1, c=6, m=3)?

Overview

Python has a lot of built-in methods and functions, which makes it easy to learn and easy to use.

The formatyear() method is used to generate a calendar for a specified period.

Let’s say you are making an application that needs to use a calendar functionality in it. The formatyear() method will come in handy for such an application.

Parameters

The formatyear() method takes the following parameters:

  1. theyear [required parameter]: Year for which the calendar should be generated, e.g., 2018.

  2. w [optional parameter, default is 2]: Width between two columns.

  3. l [optional parameter, default is 1]: The line between two rows.

  4. c [optional parameter, default is 6]: The space between two months in a column.

  5. m [optional parameter, default is 3]: The number of months to be displayed in a row.

There are many ways to display the calendar, such as plain text format or even HTML formats. However, since we want to generate the calendar in plain text format for this example, we will be calling the TextCalendar class.

#example 1
#importing the calendar module
import calendar
#initializing the textcalendar class to display our calendar as plain text
text = calendar.TextCalendar(firstweekday = 0)
print(text.formatyear(theyear = 2021))
#example 2
#this example illustrates usage of the different parameters
#importing the calendar module
import calendar
#initializing the textcalendar class to display our calendar as plain text
text = calendar.TextCalendar(firstweekday = 0)
theyear = 2021
w = 5
l = 3
c = 10
m = 1
print(text.formatyear(theyear,w, l,c,m))

Whether you are trying to embed a calendar to your application or to your website, or you are simply having fun, the formatyear() function will come in handy every time.

Free Resources