Photo Details

Source: May 2025, Task 4 (DTU 02002/02003). Study solution — not an official answer key.

Problem

Filenames look like YYYYMMDD_ID.EXT (ID and extension length may vary).

Write photo_details(filename) returning a dict with keys 'year', 'month', 'day', 'number', 'ext' (all string values; ext without the dot).

Example
>>> photo_details('20250305_110031.jpg')
{'year': '2025', 'month': '03', 'day': '05', 'number': '110031', 'ext': 'jpg'}

Approach

Slice the date prefix, then split the remainder on _ and .. The identifying number and extension are variable length.

Solution

Show solution Hide solution
Solution
def photo_details(filename):
    date_part, rest = filename.split("_", 1)
    number, ext = rest.rsplit(".", 1)
    return {
        "year": date_part[0:4],
        "month": date_part[4:6],
        "day": date_part[6:8],
        "number": number,
        "ext": ext,
    }