I don't think you need datetime
for this simple task.
You can use string slicing here to return of tuple of strings in "DD-MM"
format. You can slice everything up until the 2nd character(but not including) with [:2]
, and everything after the second character with [2:]
. Then you can just put a dash "-"
between the slices.
>>> dates = 1104, 1504, 1704, 2204
>>> tuple(f"{x[:2]}-{x[2:]}" for x in map(str, dates))
('11-04', '15-04', '17-04', '22-04')
I returned a tuple
since dates
is that type. I also converted the integers from dates
to str
using map
, since you can't slice integers. You can also use f-strings to format the slices into "DD-MM"
format.
Also I'm assuming you wanted your expected output to be a tuple of strings, since dates = 11-04, 15-04, 17-04, 22-04
alone would be invalid.
Update
If you want to extract the dates from input()
, you pass in input delimited by ","
to split into a list of strings:
>>> dates = input()
1104, 1504, 1704, 2204
>>> dates
'1104, 1504, 1704, 2204'
>>> tuple(f"{x[:2]}-{x[2:]}" for x in map(str.strip, dates.split(",")))
('11-04', '15-04', '17-04', '22-04')
Which also strips leftover whitespace with str.strip
, since inputs could possibly be 1104, 1504, 1704, 2204
, 1104,1504,1704,2204
, 1104, 1504,1704, 2204
etc. Its much easier to just split on ","
, then strip the whitespace afterwards.