Create a date object from a supported ISO 8601 date string.
How It Works
date.fromisoformat(text) parses ISO-compatible calendar dates and supported week-date forms. It returns a date only; time-of-day information is not represented.
Step-by-Step Example
from datetime import date
parsed_date = date.fromisoformat('2026-10-17')
print(parsed_date.year, parsed_date.month, parsed_date.day)
Understanding the Example
- The expression "date.fromisoformat('2026-10-17')" supplies the value stored as 'parsed_date'. This value is then used or checked by the later statements.
- The print() call exposes the result so you can compare it with the behavior described above. Check the value and its type when experimenting.
A successful run of this example prints the following output:
2026 10 17
Where This Fits in a Real Program
In a real import pipeline, parsing is only the first stage. Convert each field to the intended type, reject missing or unexpected values and report which record failed. Keep the original source file unchanged until the normalized data has passed validation and an export has been checked.
Practice: Try It Yourself
Catch ValueError when handling untrusted or malformed date strings.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Expecting a date parser to preserve hours or to accept arbitrary locale-specific date strings.
- Parse a YYYY-MM-DD input and display its ISO weekday number.
Key Takeaways
- Create a date object from a supported ISO 8601 date string.
- date.fromisoformat(text) parses ISO-compatible calendar dates and supported week-date forms.
- Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.