easy +10 pts

Month Start Date

Given a date string, return the first day of that month as a date string in the same format.

Write a function `month_start(date_str)` that takes a date string in the format `'YYYY-MM-DD'` and returns a string representing the first day of that month, also in the format `'YYYY-MM-DD'`. For example, if the input is `'2023-07-25'`, the output should be `'2023-07-01'`. You may use Python's `datetime` module or implement the logic manually. The input is guaranteed to be a valid calendar date (e.g., February 30 will not appear). Your function should handle any year from 1 to 9999 inclusive. The output must always be exactly 10 characters with zero-padded year, month, and day.

Constraints

Input is a string of exactly 10 characters in the format `YYYY-MM-DD` where `YYYY` is between 0001 and 9999, and the date is valid. The function must return a string in the same format, always with the day as '01' and zero-padded year and month.

Example

>>> month_start('2023-07-25')
'2023-07-01'
>>> month_start('2020-02-10')
'2020-02-01'
>>> month_start('2024-12-31')
'2024-12-01'
>>> month_start('0001-01-01')
'0001-01-01'
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `datetime.strptime` to parse the input and `strftime` to format the output, but be careful with year 1 formatting.
You can replace the day part with '01' after splitting the string by '-', which avoids datetime formatting issues entirely.
The output must include the year as four digits with leading zeros if needed, e.g., '0001' for year 1.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.