How to find number of weeks in a month in Python?

Recently for one of my requirement, I had to find the number of weeks in a given month including partial weeks. There are several ways to find this. The method I followed is by using the len() function around calendar.monthcalendar() method. Here is syntax and an example.

Syntax

len(calendar.monthcalendar(year, month))

Example

import calendar

Year_given = 2019

for month_no in range(1,13):
     print("\nWeeks in", calendar.month_name[month_no], Year_given, ": ",  \
          len(calendar.monthcalendar(Year_given, month_no)))
finding number of weeks in a month in Python

More Tips

Reference


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.