Tidy Dates
Advanced · Stretch · from Every Python String Method Explained with Examples
From the lesson: Every Python String Method Explained with Examples
Strings in Python come with a toolbox of built-in methods. These methods let you change case, remove extra spaces, search for words, split text apart, and much more.
Read the lessonPeople type dates in many ways. Write a function to_iso(date_text) that takes a date written month first, such as "7/4/2026", and returns it in the international year-month-day form with two-digit months and days.
to_iso("7/4/2026") # '2026-07-04'
to_iso("12/25/1999") # '1999-12-25'The text may have spaces around it, and the parts may be separated by / or by - ("7-4-2026").
The def line is written for you: put your code under it, indented four spaces, and end with return and your answer. Check calls to_iso() with its own dates, some of them hidden. (Functions get their own lesson later.)
What Check runs
Your code runs first, then each of these lines, and 4 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| to_iso("7/4/2026") | '2026-07-04' |
| to_iso("12/25/1999") | '1999-12-25' |
More String Methods exercises
- Clean Up User InputBeginner
- Count Word OccurrencesBeginner
- Replace All OccurrencesBeginner
- Split a CSV LineBeginner
- Fix the Bug: find() vs index()Beginner
- Validate a UsernameBeginner
All Python exercises · 50 practice problems · Python Fundamentals quiz