Regular expressions are a usuall solution to parse strings, and may be poorly suited to render strings back.
It should be noted that the regular f-strings (or str.format method) in Python, along with the custom text-formatting options allowed by the datetime-module can be a better choice there.
If your date is actually in a dictionary, then it can work like this:
date_dct = {"year": 2024, "month": 1, "day": 1}
message = "{year}_{month:02d}_{day:02d}".format_map(date_dct)
Or, if you get your date as a date/datetime object:
from datetime import date
date_dct = {"year": 2024, "month": 1, "day": 1}
# Parse your dict data into a proper date object:
day = date(**date_dct)
# Use the "strftime" format specification to render date using an f-string
print(f"{day:%Y_%m_%d}")
Note that in these approaches, the date information should be used as numbers, instead of strings. If they are indeed as strings and you don't want to parse the proper date, an string formatting operation can work directly as:
message = "{year}_{month}_{day}".format_map(date_dct)"
With no information needed about the number of digits for each component.
{'year': '2024', 'month': '01', 'day': '01'}?