Year Extraction from Date String

Write a Python lambda function that extracts the year from a string formatted as ‘DD-MM-YYYY’. The function should accept a string and return an integer.

Example 1:

Input: "01-01-2020"
Output: 2020

Example 2:

Input: "31-12-2023"
Output: 2023

Use Python’s string slicing technique to extract the year from the string.

# Define the lambda function
extract_year = lambda date: int(date[-4:])

# Test the function
print(extract_year("01-01-2020"))  # Output: 2020
print(extract_year("31-12-2023"))  # Output: 2023

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!