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.
The formatyear()
method takes the following parameters:
theyear
[required parameter]: Year for which the calendar should be generated, e.g., 2018.
w
[optional parameter, default is 2]: Width between two columns.
l
[optional parameter, default is 1]: The line between two rows.
c
[optional parameter, default is 6]: The space between two months in a column.
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 moduleimport calendar#initializing the textcalendar class to display our calendar as plain texttext = calendar.TextCalendar(firstweekday = 0)print(text.formatyear(theyear = 2021))
#example 2#this example illustrates usage of the different parameters#importing the calendar moduleimport calendar#initializing the textcalendar class to display our calendar as plain texttext = calendar.TextCalendar(firstweekday = 0)theyear = 2021w = 5l = 3c = 10m = 1print(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.