Editor

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 lesson

People 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.)

def to_iso(date_text):
pass
Solution unlocks after your first Check

What Check runs

Your code runs first, then each of these lines, and 4 more that stay hidden. The output must match exactly.

RunsExpected output
to_iso("7/4/2026")'2026-07-04'
to_iso("12/25/1999")'1999-12-25'

More String Methods exercises

All Python exercises · 50 practice problems · Python Fundamentals quiz