Python Get Month Name from Number Example

By Hardik Savani October 30, 2023 Category : Python

In this tutorial, I will show you python get month name from number. Here you will learn python calendar get month name from number. you will learn how to get month name from number in python. you'll learn python3 find month name from number.

In this example, I will give two examples for you of how to get the month name from number in python. therefore, let's see below example code and try it.

Example 1: using datetime

main.py

import datetime
  
monthNumber = "7"
dateTimeObject = datetime.datetime.strptime(monthNumber, "%m")
  
monthName = dateTimeObject.strftime("%b")
print("Month Short Name: ", monthName)
  
fullMonthName = dateTimeObject.strftime("%B")
print("Month Full Name: ", fullMonthName)

Output:

Month Short Name:  Jul
Month Full Name:  July

Example 2: using calendar

main.py

import calendar
  
monthNumber = 7
  
res = calendar.month_name[monthNumber]
  
print("Month Name : " + str(res))

Output:

Month Name : July

I hope it can help you...

Tags :
Shares