How to print a complete 1-year calendar in python

Share Your Love

To print a complete one-year calendar in Python, you can use the calendar module, which is part of the Python standard library. Here’s an example of how you can do it:

import calendar

# Create a calendar object for the year you want to print
year = 2023  # Change this to the desired year
cal = calendar.TextCalendar(calendar.SUNDAY)  # You can change the starting day of the week if needed

# Loop through each month and print the calendar for that month
for month in range(1, 13):  # 1 to 12 for January to December
    print(cal.formatmonth(year, month))

In this code:

  1. Import the calendar module.
  2. Define the year for which you want to print the calendar (change the year variable to your desired year).
  3. Create a TextCalendar object with the starting day of the week (you can change it to calendar.MONDAY if you want weeks to start on Monday).
  4. Use a loop to iterate through each month (from 1 to 12) and print the calendar for that month using cal.formatmonth(year, month).

Print a complete 1-year calendar in Python user input

If you want to allow the user to input the year for which they want to print the calendar, you can modify the code to take user input. Here’s an updated version of the code:

import calendar

# Prompt the user for the year
year = int(input("Enter the year (e.g., 2023): "))
cal = calendar.TextCalendar(calendar.SUNDAY)

# Loop through each month and print the calendar for that month
for month in range(1, 13):
    print(cal.formatmonth(year, month))

In this updated code:

  1. We use the input function to prompt the user to enter the year.
  2. We convert the user’s input to an integer using int() because input returns a string.
  3. The rest of the code remains the same as before, where it prints the calendar for the specified year and each month.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter