To get the month name from a number in Python, the easiest way is with strftime() and passing “%M”.

import datetime

currentDate = datetime.date.today()
currentMonthName = currentDate.strftime("%B")

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
March

You can also use the calendar module and the month_name() function.

import calendar
import datetime

currentDate = datetime.date.today()
currentMonthName = calendar.month_name[currentDate.month]

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
'March'

When working in Python, many times we need to create variables which represent dates and times. When creating and displaying values related to dates, sometimes we need to display a particular day or month.

We can get the name of a month from date or datetime object easily in Python using the strftime() function.

The Python strftime() function is very useful when working with date and datetime variables. strftime() accepts a string representing the format of a date and returns the date as a string in the given format.

To get the name of the month using strftime(), pass “%B”.

Below is a simple example of getting the month name from a date object in Python.

import datetime

currentDate = datetime.date.today()
currentMonthName = currentDate.strftime("%B")

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
March

If you just want the abbreviation of the month name, pass “%b” to strftime().

Below is a simple example of getting the month name abbreviation from a date object in Python.

import datetime

currentDate = datetime.date.today()
currentMonthName = currentDate.strftime("%b")

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
Mar

How to Get Name of Month Using Python calendar module

Another method to get the month name from a month number is using the calendar module. The Python calendar module has many great functions that make working with dates easy.

The calendar module has two built-in lists which have the month names and month abbreviations. These lists are “month_name” and “month_abbr”.

To get the name of given month, we can access the nth position of “month_name” and get the name of that month.

Below is a simple example of getting the month name using the Python calendar module in Python.

import calendar
import datetime

currentDate = datetime.date.today()
currentMonthName = calendar.month_name[currentDate.month]

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
'March'

If you instead want the abbreviation of the month name, then use the “month_abbr” list.

Below is a simple example of getting the month abbreviation using the Python calendar module in Python.

import calendar
import datetime

currentDate = datetime.date.today()
currentMonthName = calendar.month_abbr[currentDate.month]

print(currentDate)
print(currentMonthName)

#Output:
2022-03-07
'Mar'

How to Get Name of Month Using pandas in Python

If you are using pandas, you can get the month name from columns and Series with data type datetime.

Let’s say you have the following series of dates in pandas.

import pandas as pd

dates = pd.Series(['2022-03-05', '2020-01-31', '2019-03-02'])

To get the month names from a Series with datetimes, use dt.month_name()

Below is how to get the month name from a datetime using pandas.

import pandas as pd

dates = pd.Series(['2022-03-05', '2020-01-31', '2019-03-02'])

dates = pd.to_datetime(dates)

print(dates.dt.month_name(locale="English"))

#Output:
0      March
1    January
2      March
dtype: object

Hopefully this article has been helpful for you to learn how to get the month name in Python.

Categorized in:

Python,

Last Update: February 26, 2024