Python TextCalendar - formatmonth() and prmonth()


Python tip:

Python's calendar module lets you output a calendar month as a multi-line string. The required parameters are the year and the month.

You can use formatmonth to generate the string or prmonth to directly print it.

formatmonth:

import calendar

cal = calendar.TextCalendar()
month = cal.formatmonth(2022, 6)

print(month)

"""
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
"""

prmonth:

import calendar

cal = calendar.TextCalendar()

cal.prmonth(2022, 6)

"""
     June 2022
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
"""