Python Calendar prcal Function

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The prcal function in Python's calendar module prints a calendar for a specified year. This function is useful for generating and displaying a text-based calendar for an entire year directly to the console.

Table of Contents

  1. Introduction
  2. prcal Function Syntax
  3. Examples
    • Basic Usage
    • Printing a Calendar for a Specific Year
  4. Real-World Use Case
  5. Conclusion

Introduction

The prcal function in Python's calendar module prints a formatted calendar for a specified year to the console. This function is particularly useful for quickly displaying a year-long calendar without needing to handle string formatting manually.

prcal Function Syntax

Here is how you use the prcal function:

import calendar

calendar.prcal(year, w=2, l=1, c=6, m=3)

Parameters:

  • year: The year for which the calendar is to be printed (an integer).
  • w: Width between date columns. Default is 2.
  • l: Number of lines per week. Default is 1.
  • c: Space between month columns. Default is 6.
  • m: Number of months per row. Default is 3.

Returns:

  • None. The function prints the calendar directly to the console.

Examples

Basic Usage

Here is an example of how to use the prcal function to print a calendar for a specific year.

Example

import calendar

# Printing a calendar for the year 2024
calendar.prcal(2024)

Output:

                                  2024

      January                   February                   March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7                1  2  3  4                   1  2  3
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       4  5  6  7  8  9 10
15 16 17 18 19 20 21      12 13 14 15 16 17 18      11 12 13 14 15 16 17
22 23 24 25 26 27 28      19 20 21 22 23 24 25      18 19 20 21 22 23 24
29 30 31                  26 27 28 29               25 26 27 28 29 30 31

       April                      May                       June
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7             1  2  3  4  5                      1  2
 8  9 10 11 12 13 14       6  7  8  9 10 11 12       3  4  5  6  7  8  9
15 16 17 18 19 20 21      13 14 15 16 17 18 19      10 11 12 13 14 15 16
22 23 24 25 26 27 28      20 21 22 23 24 25 26      17 18 19 20 21 22 23
29 30                     27 28 29 30 31            24 25 26 27 28 29 30

        July                     August                  September
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7                1  2  3  4                         1
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       2  3  4  5  6  7  8
15 16 17 18 19 20 21      12 13 14 15 16 17 18       9 10 11 12 13 14 15
22 23 24 25 26 27 28      19 20 21 22 23 24 25      16 17 18 19 20 21 22
29 30 31                  26 27 28 29 30 31         23 24 25 26 27 28 29
                                                    30

      October                   November                  December
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6                   1  2  3                         1
 7  8  9 10 11 12 13       4  5  6  7  8  9 10       2  3  4  5  6  7  8
14 15 16 17 18 19 20      11 12 13 14 15 16 17       9 10 11 12 13 14 15
21 22 23 24 25 26 27      18 19 20 21 22 23 24      16 17 18 19 20 21 22
28 29 30 31               25 26 27 28 29 30         23 24 25 26 27 28 29
                                                    30 31

Printing a Calendar for a Specific Year

This example demonstrates how to customize the calendar output.

Example

import calendar

# Printing a calendar for the year 2025 with custom spacing
calendar.prcal(2025, w=4, l=2, c=10, m=2)

Output:

                                     2025



             January                                     February

Mon  Tue  Wed  Thu  Fri  Sat  Sun           Mon  Tue  Wed  Thu  Fri  Sat  Sun

            1    2    3    4    5                                      1    2

  6    7    8    9   10   11   12             3    4    5    6    7    8    9

 13   14   15   16   17   18   19            10   11   12   13   14   15   16

 20   21   22   23   24   25   26            17   18   19   20   21   22   23

 27   28   29   30   31                      24   25   26   27   28



              March                                       April

Mon  Tue  Wed  Thu  Fri  Sat  Sun           Mon  Tue  Wed  Thu  Fri  Sat  Sun

                           1    2                  1    2    3    4    5    6

  3    4    5    6    7    8    9             7    8    9   10   11   12   13

 10   11   12   13   14   15   16            14   15   16   17   18   19   20

 17   18   19   20   21   22   23            21   22   23   24   25   26   27

 24   25   26   27   28   29   30            28   29   30

 31



               May                                         June

Mon  Tue  Wed  Thu  Fri  Sat  Sun           Mon  Tue  Wed  Thu  Fri  Sat  Sun

                 1    2    3    4                                           1

  5    6    7    8    9   10   11             2    3    4    5    6    7    8

 12   13   14   15   16   17   18             9   10   11   12   13   14   15

 19   20   21   22   23   24   25            16   17   18   19   20   21   22

 26   27   28   29   30   31                 23   24   25   26   27   28   29

                                             30



               July                                       August

Mon  Tue  Wed  Thu  Fri  Sat  Sun           Mon  Tue  Wed  Thu  Fri  Sat  Sun

       1    2    3    4    5    6                                 1    2    3

  7    8    9   10   11   12   13             4    5    6    7    8    9   10

 14   15   16   17   18   19   20            11   12   13   14   15   16   17

 21   22   23   24   25   26   27            18   19   20   21   22   23   24

 28   29   30   31                           25   26   27   28   29   30   31



            September                                    October

Mon  Tue  Wed  Thu  Fri  Sat  Sun           Mon  Tue  Wed  Thu  Fri  Sat  Sun

  1    2    3    4    5    6    7                       1    2    3    4    5

  8    9   10   11   12   13   14             6    7    8    9   10   11   12

 15   16   17   18   19   20   21            13   14   15   16   17   18   19

 22   23   24   25   26   27   28            20   21   22   23   24   25   26

 29   30                                     27   28   29   30   31



             November                                    December

Mon  Tue  Wed  Thu  Fri  Sat  Sun           Mon  Tue  Wed  Thu  Fri  Sat  Sun

                           1    2             1    2    3    4    5    6    7

  3    4    5    6    7    8    9             8    9   10   11   12   13   14

 10   11   12   13   14   15   16            15   16   17   18   19   20   21

 17   18   19   20   21   22   23            22   23   24   25   26   27   28

 24   25   26   27   28   29   30            29   30   31

Real-World Use Case

Generating Printable Calendars

In real-world applications, the prcal function can be used to generate and display text-based calendars for a specific year, which can then be printed or used in console applications.

Example

import calendar

def display_year_calendar(year):
    calendar.prcal(year)

# Example usage
display_year_calendar(2024)

Output:

                                  2024

      January                   February                   March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7                1  2  3  4                   1  2  3
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       4  5  6  7  8  9 10
15 16 17 18 19 20 21      12 13 14 15 16 17 18      11 12 13 14 15 16 17
22 23 24 25 26 27 28      19 20 21 22 23 24 25      18 19 20 21 22 23 24
29 30 31                  26 27 28 29               25 26 27 28 29 30 31

       April                      May                       June
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7             1  2  3  4  5                      1  2
 8  9 10 11 12 13 14       6  7  8  9 10 11 12       3  4  5  6  7  8  9
15 16 17 18 19 20 21      13 14 15 16 17 18 19      10 11 12 13 14 15 16
22 23 24 25 26 27 28      20 21 22 23 24 25 26      17 18 19 20 21 22 23
29 30                     27 28 29 30 31            24 25 26 27 28 29 30

        July                     August                  September
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7                1  2  3  4                         1
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       2  3  4  5  6  7  8
15 16 17 18 19 20 21      12 13 14 15 16 17 18       9 10 11 12 13 14 15
22 23 24 25 26 27 28      19 20 21 22 23 24 25      16 17 18 19 20 21 22
29 30 31                  26 27 28 29 30 31         23 24 25 26 27 28 29
                                                    30

      October                   November                  December
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6                   1  2  3                         1
 7  8  9 10 11 12 13       4  5  6  7  8  9 10       2  3  4  5  6  7  8
14 15 16 17 18 19 20      11 12 13 14 15 16 17       9 10 11 12 13 14 15
21 22 23 24 25 26 27      18 19 20 21 22 23 24      16 17 18 19 20 21 22
28 29 30 31               25 26 27 28 29 30         23 24 25 26 27 28 29
                                                    30 31

Conclusion

The prcal function in Python's calendar module prints a formatted calendar for a specified year directly to the console. This function is useful for quickly displaying a year-long calendar without needing to handle string formatting manually, making it ideal for generating printable calendars and console applications.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare