Python TextCalendar.formatmonth()方法 (Python TextCalendar.formatmonth() Method)
formatmonth() method is an inbuilt method of the TextCalendar class of calendar module in Python. It works on text calendars and returns a multi-line string representing the calendar of the given month.
formatmonth()方法是Python中日历模块的TextCalendar类的内置方法。 它适用于文本日历,并返回代表给定月份的日历的多行字符串。
Module:
模块:
import calendar
Class:
类:
from calendar import TextCalendar
Syntax:
句法:
formatmonth(year, month, w=0, l=0)
Parameter(s):
参数:
year: It is a required parameter, which specifies the year of the calendar.
year :这是必填参数,用于指定日历的年份。
month: It is a required parameter, which specifies the month of the calendar.
month :这是必填参数,用于指定日历的月份。
w: It is an optional parameter, which specifies the width of the date column; default value = 0.
w :这是一个可选参数,用于指定日期列的宽度; 默认值= 0。
l: It is an optional parameter, which represents the number of lines one week would use in the resulting string; default value = 0.
l :这是一个可选参数,代表一周中将在结果字符串中使用的行数; 默认值= 0。
Return value:
返回值:
The return type of this method is <class 'str'>, it returns the calendar of the given month of the given year.
此方法的返回类型为<class'str'> ,它返回给定年份的给定月份的日历。
Example:
例:
# Python program to illustrate the
# use of formatmonth() method
# import class
import calendar
# creating a TextCalendar instance
cal = calendar.TextCalendar()
year = 2019
month = 12
# default width =0
print("Month's calendar:", cal.formatmonth(year, month))
print()
# varying width and length
cal = calendar.TextCalendar()
year = 1996
month = 2
# width=5, length=2
print("Month's calendar:", cal.formatmonth(year, month, 5, 2))
print()
# changing the firstweekday() for a different display
cal = calendar.TextCalendar(firstweekday=3)
# This will display the first column as Thursday
year = 1819
month = 9
# width=3
print("Month's calendar:", cal.formatmonth(year, month, 3))
print()
Output
输出量
Month's calendar: December 2019
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 31
Month's calendar: February 1996
Mon Tue Wed Thu Fri Sat Sun
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
Month's calendar: September 1819
Thu Fri Sat Sun Mon Tue Wed
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
翻译自: https://www.includehelp.com/python/textcalendar-formatmonth-method-with-example.aspx