How to Create a List of Months in Python?

Estimated read time 1 min read

To create a list of months in Python, you can use the built-in calendar module. This module provides a month_name list that contains the names of all the months in a year.

Here is an example code snippet that creates a list of months:

import calendar

months = list(calendar.month_name)[1:]

print(months)

Output:

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

In this code, we imported the calendar module and used the month_name attribute to get a list of all the month names, including an empty string at index 0. To remove the empty string, we created a new list that starts from index 1 using a slice operation ([1:]).

Finally, we printed the list of months using the print() function.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply