Sai A Sai A
Updated date May 22, 2023
In this blog, we will explain various methods to extract the year from a date in Python, including using the strftime() method, split() method, and regular expressions.

Introduction:

Python is a versatile programming language that is widely used in various applications such as web development, machine learning, data analysis, and scientific computing. It has a rich library of built-in functions and modules that make programming easier and faster. In this blog, we will discuss various ways to extract the year from a date in Python.

Method 1: Using strftime() method

The strftime() method is used to format a date and time into a string representation. It takes a format string as an argument and returns a string that represents the date and time according to the format specified. We can use this method to extract the year from a date.

import datetime

date_string = '2021-05-15'
date_object = datetime.datetime.strptime(date_string, '%Y-%m-%d')
year = date_object.strftime('%Y')
print(year)

Output:

2021

In the above program, we first import the datetime module which provides classes for working with dates and times. We then define a date string '2021-05-15' that represents the date we want to extract the year from.

Next, we use the strptime() method of the datetime class to convert the date string into a date object. The strptime() method takes two arguments - the first argument is the date string, and the second argument is the format string that specifies the format of the date string. In this case, the format string '%Y-%m-%d' specifies that the date string is in the format of year-month-day.

We then use the strftime() method of the datetime class to extract the year from the date object. The strftime() method takes a format string as an argument that specifies the format of the output string. In this case, the format string '%Y' specifies that we want to extract the year from the date object.

Method 2: Using split() method

Another way to extract the year from a date string is by using the split() method. The split() method splits a string into a list of substrings based on a delimiter. We can split the date string using the delimiter '-' and extract the year from the resulting list.

date_string = '2021-05-15'
year = date_string.split('-')[0]
print(year)

Output:

2021

In the above program, we define a date string '2021-05-15' that represents the date we want to extract the year from. We then use the split() method of the string class to split the date string into a list of substrings based on the delimiter '-'. The resulting list will have three elements - the year, month, and day.

We can then extract the year from the resulting list by accessing the first element of the list using index notation. In this case, the first element of the list is the year '2021'.

Method 3: Using regular expressions

Regular expressions are a powerful tool for working with text data. We can use regular expressions to extract the year from a date string by defining a pattern that matches the year.

import re

date_string = '2021-05-15'
year = re.search('\d{4}', date_string).group()
print(year)

Output:

2021

In the above program, we first import the re module which provides support for regular expressions in Python. We then define a date string '2021-05-15' that represents the date we want to extract the year from.

Next, we use the search() method of the re-module to search for a pattern in the date string. The pattern we use is '\d{4}', which matches any sequence of four digits. The search() method returns a match object if it finds a match for the pattern in the string. We then use the group() method of the match object to extract the matched substring, which in this case is the year '2021'.

Conclusion:

In this blog, we discussed various ways to extract the year from a date in Python. We first used the strftime() method of the datetime class to format a date object into a string representation and extract the year from the string. We then used the split() method of the string class to split a date string into a list of substrings and extract the year from the resulting list. Lastly, we used regular expressions to define a pattern that matches the year in a date string and extract the matched substring.

Comments (0)

There are no comments. Be the first to comment!!!