Convert Month Name to Number in Python

In this post, we will see how to convert Month name to number in Python.

When representing a date in Python, the user can decide what way they want to show it. The month, in general, can be represented either by its name/abbreviation or even by a number corresponding to that particular month. Therefore, it is essential to have a process to convert from one representation to another. This tutorial demonstrates the different ways available to convert month name to month number in Python.

How to convert month name to number in Python?

There are several ways to implement the task of converting month name to number in Python, ranging from creating user-defined functions to utilizing functions from pre-existing libraries, all of which have been described in the article below.

Creating a user-defined function and utilizing it to convert month name to number in Python.

We can simply create a user-defined function, where we define a dictionary that holds all the values of the month names and the numbers equivalent to them respectively. The function can then be tackled to deal with either the month names or the month name abbreviations, as per the programmer’s needs.

In our example code, we will be making the necessary changes to the code in order to deal with month name abbreviations.

The following code creates a user-defined function and uses it to convert month name to month number in Python.

The above code provides the following output:

3

Using the calendar module to create a dictionary and utilize it to convert month name to number in Python.

Python provides a built-in module calendar that enables the user to deal with all calendar operations. It provides additional functions and classes related to calendar operations.

The calendar module can simply be utilized to create a dictionary that converts and stores the data of conversion of the month name to month number in Python. This method also utilizes the enumerate() function.

The following code uses the calendar module to create a dictionary and utilize it to convert month name to number in Python.

The above code provides the following output:

{‘Jan’: 1, ‘Feb’: 2, ‘Mar’: 3, ‘Apr’: 4, ‘May’: 5, ‘Jun’: 6, ‘Jul’: 7, ‘Aug’: 8, ‘Sep’: 9, ‘Oct’: 10, ‘Nov’: 11, ‘Dec’: 12}

Using dict comprehension and the calendar module to convert month name to number in Python.

Dict comprehension is a compact and efficient way to create dictionaries in Python. Chunks of dictionary code can be minimized to a single line with the help of this method. It works similar to the more popular list comprehensions in Python.

The dict comprehension feature was introduced in Python 2.7 and is accepted in all the versions released after it.

The following code uses dict comprehension and the calendar module to convert month name to number in Python.

The above code provides the following output:

{‘Jan’: 1, ‘Feb’: 2, ‘Mar’: 3, ‘Apr’: 4, ‘May’: 5, ‘Jun’: 6, ‘Jul’: 7, ‘Aug’: 8, ‘Sep’: 9, ‘Oct’: 10, ‘Nov’: 11, ‘Dec’: 12}

Using the strptime() function from the datetime module to convert month name to number in Python.

The strptime() function is utilized to convert a string object into a datetime object, given the string follows a specified format.

This function can be utilized to deal with both month names and month abbreviations and is able to successfully implement the task of converting month names to numbers in Python.

We will give an example to convert both of these types to number in Python. The month name can be converted by utilizing the ‘%B’ sign as an argument to the strptime() function while the three-letter month abbreviation can be converted by utilizing the ‘%b’ sign as an argument to the strptime() function.

First, let us look at how we can convert month name to number by utilizing the strptime() function.

The above code provides the following output:

Moving on, let us now consider making simple tweaks to the above code in order to make it applicable on three-letter month abbreviations as well.

The following code uses the strptime() function from the datetime module to convert month name abbreviations to numbers in Python.

The above code provides the following output:

That’s all about how to convert month name to number in Python.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 18 May

    Get Hour from datetime in Python

    Table of ContentsHow to get hour from datetime in Python?Using the time() function and manually slicing out the hour part to get hour from datetime in Python.Using the % operator along with the datetime.hour attribute to get hour from datetime in Python.Using the format() function along with the datetime.hour attribute to get hour from datetime […]

  • 18 May

    Add Hours to datetime in Python

    Table of Contentsdatetime in PythonWays to add hours to datetime in PythonUsing the datetime.Hour Attribute to Add Hours to datetime in PythonUsing the datetime.timedelta object to add hours to datetime in PythonUsing the dateutil.Relativedelta Object to Add Hours to datetime in PythonUsing the pandas.Date Offset() Function to Add Hours to datetime in PythonUsing the arrow.shift() […]

  • Format Date to YYYYMMDD in Python
    03 March

    Format Date to YYYYMMDD in Python

    Table of ContentsDate formats in PythonHow to convert date to YYYYMMDD format in PythonUsing datetime.strftime() Function to Format Date to YYYYMMDD in PythonUsing datetime.date Object to Format Date to YYYYMMDD in PythonUsing date.isoformat() Function to Format Date to YYYYMMDD in PythonUsing arrow.format() Function to Format Date to YYYYMMDD in PythonUsing pendulum.to_date_string() Function to Format Date […]

  • Get today's date in Python
    03 March

    Get Today’s Date in Python

    Table of ContentsDate and Time objects in PythonGet Today’s Date in PythonUsing date.today() Function to Get Today’s Date in PythonUsing datetime.today() and datetime.strftime() Functions to Get Today’s Date in PythonUsing datetime.now() Function to Get Today’s Date in PythonUsing strftime() Function to Get Today’s Date in PythonUsing time.strftime() Function to Get Today’s Date in PythonUsing arrow.now() […]

  • Add seconds to datetime in Python
    11 February

    Add Seconds to Datetime in Python

    Table of ContentsDate and Time in PythonWays to add seconds to datetime object in PythonUsing the datetime.timedelta object to add seconds to datetime in PythonUsing the datetime.time.second attribute to add seconds to datetime in PythonUsing the arrow.shift() function to add seconds to arrow object in PythonConclusionWas this post helpful? Date and Time in Python Python […]

  • Python get year from date
    23 January

    Get year from Date in Python

    Table of ContentsDates in PythonWays to get year from date stored in a datetime object in PythonUsing the year attributeUsing the strftime() functionUsing the pandas.to_datetime functionWays to get year from date stored as a string in PythonUsing the for loopUsing the re.search()ConclusionWas this post helpful? In this post, we will see how to get year […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.