What the Standard Library provides
There is no direct scanf(3) equivalent in the standard library. The documentation for the re
module suggests itself as a replacement, providing this explanation:
Python does not currently have an equivalent to scanf(). Regular expressions are generally more powerful, though also more verbose, than scanf() format strings. The table below offers some more-or-less equivalent mappings between scanf() format tokens and regular expressions.
Modifying your hypothetical example, to work with regular expressions, we get...
import re
input_date_list = re.match(r"(?P<month>[-+]?\d+)-(?P<day>[-+]?\d+)-(?P<year>[-+]?\d+)", input())
print(f"[{input_date_list['year']}, {input_date_list['month']}, {input_date_list['day']}]")
input_time_list = re.match(r"(?P<hour>[-+]?\d+):(?P<minute>[-+]?\d+)", input())
print(f"[{input_time_list['hour']}, {input_time_list['minute']}]")
...and when executed:
python script.py << EOF
1-2-2023
11:59
EOF
[2023, 1, 2]
[11, 59]
Community Implementation
The scanf module on Pypi provides an interface much more akin to scanf(3).
This provides a direct implementation of your hypothetical examples:
from scanf import scanf
input_date_list = scanf("%d-%d-%d")
input_time_list = scanf("%d:%d")
print(input_date_list, input_time_list)
...and when executed:
python script.py << EOF
1-2-2023
11:59
EOF
(1, 2, 2023) (11, 59)