Python Glossary Русский

all Python 3calendarterm

calendar.TextCalendar

calendar classes

A plain-text calendar: formatmonth() and formatyear() return a string, while prmonth() and pryear() print it straight away.

Syntax

tc = calendar.TextCalendar(firstweekday=0)

The format* methods return a string, the pr* methods print and return None — print(tc.prmonth(...)) adds a stray None. Month and day names come from the process locale: until the program calls locale.setlocale() they are English, and after setlocale(LC_ALL, '') they suddenly follow the user's language. The column width is w, the number of blank lines between weeks is l.

Examples

import calendar
tc = calendar.TextCalendar(firstweekday=calendar.SUNDAY)
lines = tc.formatmonth(2024, 2).splitlines()
print(lines[0].strip())  # → February 2024
print(lines[1])  # → Su Mo Tu We Th Fr Sa
import calendar
tc = calendar.TextCalendar()
result = tc.formatweekheader(3)
print(result)  # → Mon Tue Wed Thu Fri Sat Sun

See also

Open in the full glossarydocs.python.orgReport an inaccuracy