How to Create a List of All Months in Python?

Estimated read time 1 min read

In Python, you can create a list of all months by using the calendar module, which provides various classes and functions to work with calendars. Here’s an example:

import calendar

# Create a list of all months
all_months = list(calendar.month_name)[1:]
print(all_months)

In this example, we first import the calendar module. We then use the month_name attribute provided by the calendar module to get a list of month names, including a blank string at the beginning.

To exclude the blank string, we slice the list from the second element ([1:]) and convert it to a list using the list() function. We assign this list to the variable all_months and print it using the print() function.

The output of this program would be:

['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

This is a list of all months in English. If you want to create a list of months in a different language, you can use the month_name attribute of the locale class provided by the calendar module. You would need to set the appropriate locale using the setlocale() function from the locale module.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply