Python string to datetime: Parsing dates with strptime

Python string to datetime: Parsing dates with strptime

Want to parse a Python string into a datetime? Use strptime, passing (a) the string and (b) the date format specification:

datetime.datetime.strptime('2026-04-01', '%Y-%m-%d')
datetime.datetime.strptime('26-April-01', '%y-%B-%d')

Both return datetime.datetime(2026, 4, 1, 0, 0)