How do we determine the number of days for a given month in python [duplicate]

You should use calendar.monthrange:

>>> from calendar import monthrange
>>> monthrange(2011, 2)
(1, 28)

Just to be clear, monthrange supports leap years as well:

>>> from calendar import monthrange
>>> monthrange(2012, 2)
(2, 29)

As @mikhail-pyrev mentions in a comment:

First number is the weekday of the first day of the month, the second number is the number of days in said month.

Leave a Comment