How to print calendar.monthname array in python?
I have the following code:
import calendar
month=calendar.month_name
print(month)
I want to have the following output, which is in array form or list form:
['January','February',...,'December']
but above the code nothing can be returned to me.
Can anyone please point out my error?
source to share
What it returns is actually an array object . If you want to convert it to a list, call on it: list()
>>> import calendar
>>> calendar.month_name
<calendar._localized_month instance at 0x10e0b2830>
>>> list(calendar.month_name)
['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
By default, a list has an empty string as its first element, so indexing can be done from 1 instead of 0. If you want to remove that, just do list(calendar.month_name[1:])
Depending on your code, there really may not be any reason for this. The array object will have the same properties as any list:
>>> for month in calendar.month_name[1:]:
... print month
...
January
February
March
April
May
June
July
August
September
October
November
December
source to share