In this post we will be using Python to print a formatted text calendar for a specified month. To achieve this we will utilize the Calendar module. The Python calendar
module is a handy toolkit for managing calendars in code. It helps display calendars in various formats, check weekdays, leap years, and validate dates. It supports easy navigation through weeks and months, simplifying calendar-based operations. With constants for weekdays and months, it’s a valuable resource for generating calendars and handling dates in Python projects.
See the sample of Python code below where we use the Python Calendar module to print a formatted text calendar for a specified month. We start by importing the calendar
module in Python. Next our two variables year
and month
are defined. Finally we call the call calendar.prmonth(year, month)
function which prints a textual calendar for the specified month to the console. The function below does not require the need for print()
as it directly prints the formatted calendar to the console.
import calendar
year = 2024
month = 1 # January
# Display the calendar for January 2024
calendar.prmonth(year, month)
The above would print the following to output.
Be sure to take a look at some of our other content around the Python programming language here.