{"id":25999,"date":"2021-12-07T18:06:42","date_gmt":"2021-12-07T12:36:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=25999"},"modified":"2021-12-08T20:38:09","modified_gmt":"2021-12-08T15:08:09","slug":"python-program-for-calendar-formatmonth-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-calendar-formatmonth-method-with-examples\/","title":{"rendered":"Python Program for calendar formatmonth() Method with Examples"},"content":{"rendered":"
Calendar Module:<\/strong><\/p>\n The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.<\/p>\n formatmonth() Method:<\/strong><\/p>\n To obtain a month’s calendar in a multi-line string, use the formatmonth() method.<\/p>\n To generate plain text calendars, class calendar.TextCalendar(firstweekday=0) can be used.The formatmonth() method is one of the TextCalendar instance’s methods.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:<\/strong><\/p>\n year:<\/strong> This is required. It is a number. It is the year for which the calendar should be created.<\/p>\n month:<\/strong> This is required. It is a number. It is the month for which the calendar should be created.<\/p>\n width:<\/strong> This is Optional. It is a number. It Specifies the width of the centered date columns. The default value is zero.<\/p>\n lines:<\/strong> This is Optional. It is a number. It Specifies the number of lines that each week will use. The default value is zero.<\/p>\n Return Value:<\/strong> It returns a calendar for the month.<\/p>\n Example1:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Calendar Module: The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely. formatmonth() Method: To obtain a month’s calendar in a multi-line string, use the formatmonth() method. To generate …<\/p>\nformatmonth(year, month, width=0, lines=0)<\/pre>\n
Program for calendar formatmonth() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import calendar module using the import keyword.\r\nimport calendar\r\n# Give the year as static input and store it in a variable.\r\ngvn_yr = 2012\r\n# Give the month as static input and store it in another variable.\r\ngvn_mont = 8\r\n# Call the TextCalendar() function by setting firstweekday=0 and store it in\r\n# another variable.\r\ntxt_calndr = calendar.TextCalendar(firstweekday=0)\r\n# Apply formatmonth() method to the above text calendar by passing the given year,\r\n# month as the arguments and store it in another variable.\r\nrslt = txt_calndr.formatmonth(gvn_yr, gvn_mont)\r\n# Print the above result.\r\nprint(rslt)<\/pre>\n
August 2012\r\nMo Tu We Th Fr Sa Su\r\n1 2 3 4 5\r\n6 7 8 9 10 11 12\r\n13 14 15 16 17 18 19\r\n20 21 22 23 24 25 26\r\n27 28 29 30 31<\/pre>\n
\n
# Import calendar module using the import keyword.\r\nimport calendar\r\n# Give the year as static input and store it in a variable.\r\ngvn_yr = 2019\r\n# Give the month as static input and store it in another variable.\r\ngvn_mont = 11\r\n# Give the width as static input and store it in another variable.\r\ngvn_widt = 5\r\n# Give the no of lines as static input and store it in another variable.\r\ngvn_lines = 3\r\n# Call the TextCalendar() function by setting firstweekday=0 and store it in\r\n# another variable.\r\ntxt_calndr = calendar.TextCalendar(firstweekday=0)\r\n# Apply formatmonth() method to the above text calendar by passing the given year,\r\n# month, width, no of lines as the arguments and store it in another variable.\r\nrslt = txt_calndr.formatmonth(gvn_yr, gvn_mont, gvn_widt, gvn_lines)\r\n# Print the above result.\r\nprint(rslt)\r\n<\/pre>\n
November 2019\r\n\r\n\r\n Mon Tue Wed Thu Fri Sat Sun\r\n\r\n\r\n 1 2 3\r\n\r\n\r\n 4 5 6 7 8 9 10\r\n\r\n\r\n 11 12 13 14 15 16 17\r\n\r\n\r\n 18 19 20 21 22 23 24\r\n\r\n\r\n 25 26 27 28 29 30\r\n<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import calendar module using the import keyword.\r\nimport calendar\r\n# Give the year as user input using the int(input()) function and store it in a variable.\r\ngvn_yr = int(input(\"Enter some random year = \"))\r\n# Give the month as user input using the int(input()) function and store it in another variable.\r\ngvn_mont = int(input(\"Enter some random month = \"))\r\n# Call the TextCalendar() function by setting firstweekday=0 and store it in\r\n# another variable.\r\ntxt_calndr = calendar.TextCalendar(firstweekday=0)\r\n# Apply formatmonth() method to the above text calendar by passing the given year,\r\n# month as the arguments and store it in another variable.\r\nrslt = txt_calndr.formatmonth(gvn_yr, gvn_mont)\r\n# Print the above result.\r\nprint(rslt)<\/pre>\n
Enter some random year = 2020\r\nEnter some random month = 3\r\nMarch 2020\r\nMo Tu We Th Fr Sa Su\r\n1\r\n2 3 4 5 6 7 8\r\n9 10 11 12 13 14 15\r\n16 17 18 19 20 21 22\r\n23 24 25 26 27 28 29\r\n30 31<\/pre>\n
\n
# Import calendar module using the import keyword.\r\nimport calendar\r\n# Give the year as user input using the int(input()) function and store it in a variable.\r\ngvn_yr = int(input(\"Enter some random year = \"))\r\n# Give the month as user input using the int(input()) function and store it in another variable.\r\ngvn_mont = int(input(\"Enter some random month = \"))\r\n# Give the width as user input using the int(input()) and store it in another variable.\r\ngvn_widt = int(input(\"Enter some random number = \"))\r\n# Give the no of lines user input using the int(input()) and store it in another variable.\r\ngvn_lines = int(input(\"Enter some random number = \"))\r\n# Call the TextCalendar() function by setting firstweekday=0 and store it in\r\n# another variable.\r\ntxt_calndr = calendar.TextCalendar(firstweekday=0)\r\n# Apply formatmonth() method to the above text calendar by passing the given year,\r\n# month, width, no of lines as the arguments and store it in another variable.\r\nrslt = txt_calndr.formatmonth(gvn_yr, gvn_mont, gvn_widt, gvn_lines)\r\n# Print the above result.\r\nprint(rslt)\r\n<\/pre>\n
Enter some random year = 2021\r\nEnter some random month = 5\r\nEnter some random number = 2\r\nEnter some random number = 2\r\nMay 2021\r\n\r\nMo Tu We Th Fr Sa Su\r\n\r\n1 2\r\n\r\n3 4 5 6 7 8 9\r\n\r\n10 11 12 13 14 15 16\r\n\r\n17 18 19 20 21 22 23\r\n\r\n24 25 26 27 28 29 30\r\n\r\n31<\/pre>\n