Python, properly · Chapter 36 of 42
Dates and regex
By the end of this chapter you can parse, format and subtract dates and times, keep India time and UTC straight with zoneinfo, and use regular expressions to check, search and clean messy text. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Pythonic and typed
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.
Why you need this
The payment partner wants every order's time in UTC, and last week's report was off by five and a half hours. Bhavna wants to know how long deliveries take, and a delivery that crossed midnight showed up as a few minutes. Rafiq cannot match payments to customers because customers.csv writes the same kind of phone number four different ways.
Dates look like text but are not: they have time zones, days of different lengths, and arithmetic. Messy text needs more than split and replace: it needs patterns. Python has both built in: datetime and zoneinfo, and re.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
india_time.py: India time, and UTC
orders.csv writes times like 2026-09-01 13:41, in India time, with no time zone. Write parse_placed(text), an aware datetime for that text in the time zone Asia/Kolkata (from zoneinfo), and to_utc(moment), the same moment in UTC.
to_utc(parse_placed of 2026-09-01 13:41) is 08:11 UTC on 1 September (India is 5 hours 30 minutes ahead), and 2026-09-30 02:10 in India is 20:40 UTC on 29 September. The UTC result must equal the India time it came from.
- Task 2
durations.py: how long deliveries take
Write delivery_minutes(placed, delivered): the whole minutes between two times written like 2026-09-01 13:41 (13:41 to 14:35 is 54; a delivery that took a day and five minutes is 1445).
Then delivery_report(path): for the orders CSV at path, over every order with a delivered_at, a dict with average (the average minutes, rounded to 1 decimal), slowest (the order id of the slowest delivery) and over_45 (how many took more than 45 minutes). The check runs it on /workspace/data/orders.csv.
- Task 3
phones.py: one way to write a phone number
customers.csv has mobile numbers written four ways: 07089496252, 83262-72722, 8709470176 and +91 84641 29694. Write normalise_phone(text) with a regular expression: any of the four forms becomes +91 and the ten digits, like +917089496252. A mobile number has ten digits and starts with 6, 7, 8 or 9; anything else returns None.
The check normalises every phone in customers.csv, then tries wrong ones: 12345, +91 12345 67890, 98765-4321, 98765432101, phone: 9876543210, an extension after the number, a US number and an empty string.
- Task 4 · interview question
Interview: rider_message.py, reading the riders' updates
Riders send WhatsApp updates like R3 delivered D0042 at 14:05. Write parse_update(text): a dict with rider, order and time (R3, D0042, 14:05) for an update in exactly that form, or None. Any capitals and any number of spaces between and around the words are fine; the rider and order come back in capitals; the hour may have one digit. A different verb, a missing time or words after the time give None.
Then find_orders(text): every order id in the text (D and exactly four digits, not part of a longer word), in order, repeats included. XD00421 and D00421 are not order ids; (D0099) is.
What you will learn
- datetime, date and timedelta: making, formatting (strftime) and parsing (strptime) dates and times.
- Time zones: naive and aware datetimes, ZoneInfo("Asia/Kolkata"), attaching a zone and converting to another.
- Durations: subtracting datetimes, and the .seconds trap.
- Regular expressions: search, match, fullmatch, groups, named groups, findall, sub, flags, greedy matches.
More in Pythonic and typed
Build "Dates and regex" yourself
DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.