The Python calendar.month() function is used to generate a month's calendar in a multi-line string format.
This function returns a formatted month calendar as a string, making it easy to display or print a specific month's calendar.
SyntaxFollowing is the syntax of the Python calendar.month() function −
calendar.month(year, month, w=2, l=1)Parameters
This function accepts the following parameters −
This function returns a multi-line string containing the formatted calendar for the specified month.
Example: Displaying a Month CalendarIn this example, we generate a calendar for March 2025 using the Python calendar.month() function −
import calendar # Generate March 2025 calendar month_calendar = calendar.month(2025, 3) print(month_calendar)
Following is the output obtained −
March 2025 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 31Example: Customizing Column Width
We can adjust the width of date columns using the w parameter of the month() function −
import calendar # Generate March 2025 calendar with custom width month_calendar = calendar.month(2025, 3, w=4) print(month_calendar)
Following is an excerpt of the customized output −
March 2025 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 ...Example: Adjusting Line Spacing
We can modify the line spacing between weeks using the l parameter −
import calendar # Generate March 2025 calendar with extra spacing month_calendar = calendar.month(2025, 3, l=2) print(month_calendar)
Following is output of the above code −
March 2025 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...Example: Displaying a Different Month
We can also use the month() function to display any other month, such as December 2024 −
import calendar # Generate December 2024 calendar month_calendar = calendar.month(2024, 12) print(month_calendar)
We get the output as shown below −
December 2024 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
python_date_time.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4