Python: Display a simple, formatted calendar of a given year and month
Python Datetime: Exercise-48 with Solution
Write a Python program to display a simple, formatted calendar of a given year and month.
Sample Solution:
Python Code:
# Import the calendar module
import calendar
# Print a prompt indicating that a calendar for a specific year and month will be printed
print('Print a calendar for a year and month:')
# Prompt the user to input the month and year
month = int(input('Month (mm): '))
year = int(input('Year (yyyy): '))
# Print a new line for formatting purposes
print('\n')
# Set Sunday as the first day of the week
calendar.setfirstweekday(calendar.SUNDAY)
# Generate the month calendar for the specified year and month
cal = calendar.monthcalendar(year, month)
# If the month is single-digit, prepend a '0' to it for consistency
if len(str(month)) == 1:
month = '0%s' % month
# Print the header of the calendar
print('|++++++ %s-%s +++++|' % (month, year))
print('|Su Mo Tu We Th Fr Sa|')
print('|--------------------|')
# Display the calendar
border = '|'
for week in cal:
line = border
# Iterate over each day in the week
for day in week:
# If the day is 0, it's a blank day
if day == 0:
# Print 3 spaces for blank days
line += ' '
# If the day is a single-digit number, prepend a space
elif len(str(day)) == 1:
line += ' %d ' % day
# If the day is a double-digit number, print it directly
else:
line += '%d ' % day
# Remove the extra space in the last column
line = line[0:len(line) - 1]
line += border
# Print the line
print(line)
# Print the bottom border of the calendar
print('|--------------------|\n')
Output:
Print a calendar for a year and month: Month (mm): 05 Year (yyyy): 2017 |++++++ 05-2017 +++++| |Su Mo Tu We Th Fr Sa| |--------------------| | 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 31 | |--------------------|
Explanation:
In the exercise above,
- The code imports the "calendar" module.
- It prints a prompt indicating that a calendar for a specific year and month will be printed.
- It prompts the user to input the month and year.
- It prints a new line for formatting purposes.
- It sets Sunday as the first day of the week.
- It generates the month calendar for the specified year and month.
- If the month is a single-digit number, it prepends a '0' to it for consistency.
- It prints the header of the calendar.
- It iterates over each week in the calendar, constructing a string representation of each week.
- Within each week, it iterates over each day, formatting each day according to its value (blank, single-digit, or double-digit).
- It removes the extra space in the last column of each line.
- It prints the constructed line.
- It prints the bottom border of the calendar.
Flowchart:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program display a list of the dates for the 2nd Saturday of every month for a given year.
Next: Write a Python program to convert a string into datetime.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/date-time-exercise/python-date-time-exercise-48.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics