;

How to Get a Month Name using the Month Number in Python


Tutorialsrack 01/04/2020 Python

In this article, we will learn how to get a month's name using the month number in python. There are various ways to get a month's name using the month number in python. Here are some examples as follow:

Example 1: In this example, we will use calendar.month_name[index] from the calendar module to get the full month name using the month number.

Example 1
# Import Module
import calendar

def Month_Name_Using_Month_Number(Month_number):
    Month_name=calendar.month_name[Month_number]
    return Month_name

print ("\nFull Month Name: ",Month_Name_Using_Month_Number(5))
# Output ==> Full Month Name:  May

print ("\nFull Month Name: ",Month_Name_Using_Month_Number(12))
# Output ==> Full Month Name:  December

Example 2: In this example, we will use calendar.month_abbr[index] from the calendar module to get the short month name or month name abbreviation using month number.

Example 2
# Import Module
import calendar

def Month_Name_Using_Month_Number(Month_number):
    Month_name=calendar.month_abbr[Month_number]
    return Month_name

print ("\nShort Month Name: ",Month_Name_Using_Month_Number(5))
# Output ==> Short Month Name:  May

print ("\nShort Month Name: ",Month_Name_Using_Month_Number(12))
# Output ==> Short Month Name:  Dec

Example 3: In this example, we will use the strftime('%B') date formatting method from datetime module to get the full month name using the month number.

Example 3
# Import Module
import datetime

def Month_Name_Using_Month_Number(Month_number):
    Month_name=datetime.date(1900, Month_number, 1).strftime('%B')
    return Month_name

print ("\nFull Month Name: ",Month_Name_Using_Month_Number(5))
# Output ==> Full Month Name:  May

print ("\nFull Month Name: ",Month_Name_Using_Month_Number(12))
# Output ==> Full Month Name:  December

Example 4: In this example, we will use the strftime('%b') date formatting method from datetime module to get the short month name or month name abbreviation using the month number.

Example 4
# Import Module
import datetime

def Month_Name_Using_Month_Number(Month_number):
    Month_name=datetime.date(1900, Month_number, 1).strftime('%b')
    return Month_name

print ("\nShort Month Name: ",Month_Name_Using_Month_Number(5))
# Output ==> Short Month Name:  May

print ("\nShort Month Name: ",Month_Name_Using_Month_Number(12))
# Output ==> Short Month Name:  Dec

I hope this article will help you to understand how to get a month's name using the month number in python.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags