{"id":26009,"date":"2021-12-07T18:06:49","date_gmt":"2021-12-07T12:36:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=26009"},"modified":"2021-12-08T20:41:17","modified_gmt":"2021-12-08T15:11:17","slug":"python-program-for-calendar-formatyear-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-calendar-formatyear-method-with-examples\/","title":{"rendered":"Python Program for calendar formatyear() 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 formatyear() Method:<\/strong><\/p>\n The formatyear() method returns a multi-line string containing an m-column calendar for a full year.<\/p>\n To generate plain text calendars, class calendar.TextCalendar(firstweekday=0) can be used.The formatyear() 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 width:<\/strong> This is Optional. It is a number. The distance between two columns. 2<\/strong> is the default value.<\/p>\n lines:<\/strong> This is Optional. It is a number. A blank line between two rows. 1<\/strong> is the default value.<\/p>\n c:<\/strong> This is Optional. It is a number. It is the space between two months (column-wise). 6<\/strong> is the default value.<\/p>\n m:<\/strong> This is Optional. It is a number. It is the number of months in a row. 3 is the default value.<\/p>\n Return Value:<\/strong> A m-column calendar for the whole year is returned.<\/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 <\/p>\n Example2:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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. formatyear() Method: The formatyear() method returns a multi-line string containing an m-column calendar for a full year. …<\/p>\nformatyear(year, width=2, lines=1, c=6, m=3)<\/pre>\n
Program for calendar formatyear() 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 = 2015\r\n# Give the width as static input and store it in another variable.\r\ngvn_widt = 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 formatyear() method to the above text calendar by passing the given year,\r\n# width,as the arguments and store it in another variable.\r\nrslt = txt_calndr.formatyear(gvn_yr, gvn_widt)\r\n# Print the above result.\r\nprint(rslt)\r\n<\/pre>\n
2015\r\n\r\n January February March\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 1 1\r\n 5 6 7 8 9 10 11 2 3 4 5 6 7 8 2 3 4 5 6 7 8\r\n 12 13 14 15 16 17 18 9 10 11 12 13 14 15 9 10 11 12 13 14 15\r\n 19 20 21 22 23 24 25 16 17 18 19 20 21 22 16 17 18 19 20 21 22\r\n 26 27 28 29 30 31 23 24 25 26 27 28 23 24 25 26 27 28 29\r\n 30 31\r\n\r\n April May June\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 5 1 2 3 1 2 3 4 5 6 7\r\n 6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14\r\n 13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21\r\n 20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28\r\n 27 28 29 30 25 26 27 28 29 30 31 29 30\r\n\r\n July August September\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 5 1 2 1 2 3 4 5 6\r\n 6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13\r\n 13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20\r\n 20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27\r\n 27 28 29 30 31 24 25 26 27 28 29 30 28 29 30\r\n 31\r\n\r\n October November December\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 1 1 2 3 4 5 6\r\n 5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13\r\n 12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20\r\n 19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27\r\n 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31\r\n 30<\/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 = 2020\r\n# Give the width as static input and store it in another variable.\r\ngvn_widt = 4\r\n# Give the no of lines as static input and store it in another variable.\r\ngvn_lines = 1\r\n# Give the c value as static input and store it in another variable.\r\ngvn_c_val = 3\r\n# Give the m value as static input and store it in another variable.\r\ngvn_m_val = 2\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 formatyear() method to the above text calendar by passing the given year,\r\n# width, lines, c, m values as the arguments and store it in another variable.\r\nrslt = txt_calndr.formatyear(gvn_yr, gvn_widt, gvn_lines, gvn_c_val, gvn_m_val)\r\n# Print the above result.\r\nprint(rslt)\r\n<\/pre>\n
2020\r\n\r\n January February\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 5 1 2\r\n 6 7 8 9 10 11 12 3 4 5 6 7 8 9\r\n 13 14 15 16 17 18 19 10 11 12 13 14 15 16\r\n 20 21 22 23 24 25 26 17 18 19 20 21 22 23\r\n 27 28 29 30 31 24 25 26 27 28 29\r\n\r\n March April\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 1 2 3 4 5\r\n 2 3 4 5 6 7 8 6 7 8 9 10 11 12\r\n 9 10 11 12 13 14 15 13 14 15 16 17 18 19\r\n 16 17 18 19 20 21 22 20 21 22 23 24 25 26\r\n 23 24 25 26 27 28 29 27 28 29 30\r\n 30 31\r\n\r\n May June\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 1 2 3 4 5 6 7\r\n 4 5 6 7 8 9 10 8 9 10 11 12 13 14\r\n 11 12 13 14 15 16 17 15 16 17 18 19 20 21\r\n 18 19 20 21 22 23 24 22 23 24 25 26 27 28\r\n 25 26 27 28 29 30 31 29 30\r\n\r\n July August\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 5 1 2\r\n 6 7 8 9 10 11 12 3 4 5 6 7 8 9\r\n 13 14 15 16 17 18 19 10 11 12 13 14 15 16\r\n 20 21 22 23 24 25 26 17 18 19 20 21 22 23\r\n 27 28 29 30 31 24 25 26 27 28 29 30\r\n 31\r\n\r\n September October\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 5 6 1 2 3 4\r\n 7 8 9 10 11 12 13 5 6 7 8 9 10 11\r\n 14 15 16 17 18 19 20 12 13 14 15 16 17 18\r\n 21 22 23 24 25 26 27 19 20 21 22 23 24 25\r\n 28 29 30 26 27 28 29 30 31\r\n\r\n November December\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 1 2 3 4 5 6\r\n 2 3 4 5 6 7 8 7 8 9 10 11 12 13\r\n 9 10 11 12 13 14 15 14 15 16 17 18 19 20\r\n 16 17 18 19 20 21 22 21 22 23 24 25 26 27\r\n 23 24 25 26 27 28 29 28 29 30 31\r\n 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 width as user input using the int(input()) function and store it in another variable.\r\ngvn_widt = 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 formatyear() method to the above text calendar by passing the given year,\r\n# width,as the arguments and store it in another variable.\r\nrslt = txt_calndr.formatyear(gvn_yr, gvn_widt)\r\n# Print the above result.\r\nprint(rslt)\r\n<\/pre>\n
Enter some random year = 2014\r\nEnter some random number = 4\r\n 2014\r\n\r\n January February March\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 5 1 2 1 2\r\n 6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9\r\n 13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16\r\n 20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23\r\n 27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30\r\n 31\r\n\r\n April May June\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 5 6 1 2 3 4 1\r\n 7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8\r\n 14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15\r\n 21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22\r\n 28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29\r\n 30\r\n\r\n July August September\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7\r\n 7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14\r\n 14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21\r\n 21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28\r\n 28 29 30 31 25 26 27 28 29 30 31 29 30\r\n\r\n October November December\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 5 1 2 1 2 3 4 5 6 7\r\n 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14\r\n 13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21\r\n 20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28\r\n 27 28 29 30 31 24 25 26 27 28 29 30 29 30 31\r\n\r\n<\/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 width as user input using the int(input()) function and store it in another variable.\r\ngvn_widt = int(input(\"Enter some random number = \"))\r\n# Give the no of lines as user input using the int(input()) function and store it in another variable.\r\ngvn_lines = int(input(\"Enter some random number = \"))\r\n# Give the c value as user input using the int(input()) function and store it in another variable.\r\ngvn_c_val = int(input(\"Enter some random number = \"))\r\n# Give the m value as user input using the int(input()) function and store it in another variable.\r\ngvn_m_val = 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 formatyear() method to the above text calendar by passing the given year,\r\n# width, lines, c, m values as the arguments and store it in another variable.\r\nrslt = txt_calndr.formatyear(gvn_yr, gvn_widt, gvn_lines, gvn_c_val, gvn_m_val)\r\n# Print the above result.\r\nprint(rslt)\r\n<\/pre>\n
Enter some random year = 2012\r\nEnter some random number = 3\r\nEnter some random number = 1\r\nEnter some random number = 3\r\nEnter some random number = 3\r\n 2012\r\n\r\n January February March\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 1 2 3 4 5 1 2 3 4\r\n 2 3 4 5 6 7 8 6 7 8 9 10 11 12 5 6 7 8 9 10 11\r\n 9 10 11 12 13 14 15 13 14 15 16 17 18 19 12 13 14 15 16 17 18\r\n 16 17 18 19 20 21 22 20 21 22 23 24 25 26 19 20 21 22 23 24 25\r\n 23 24 25 26 27 28 29 27 28 29 26 27 28 29 30 31\r\n 30 31\r\n\r\n April May June\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 1 2 3 4 5 6 1 2 3\r\n 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10\r\n 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17\r\n 16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24\r\n 23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30\r\n 30\r\n\r\n July August September\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 1 2 3 4 5 1 2\r\n 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9\r\n 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16\r\n 16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23\r\n 23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30\r\n 30 31\r\n\r\n October November December\r\nMon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun\r\n 1 2 3 4 5 6 7 1 2 3 4 1 2\r\n 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9\r\n 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16\r\n 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23\r\n 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30\r\n 31\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"