0Pricing
Python Academy · Lesson

Formatting and Parsing

Use strftime and strptime.

Formatting and Parsing is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why format and parse?

Computers store dates as objects, but humans read them as text. Formatting turns a datetime into a string, and parsing turns a string back into a datetime.

  • strftime = string FROM time (format).
  • strptime = string PARSE time (parse).
from datetime import datetime
now = datetime(2026, 5, 30, 14, 5)
print(now)

Your first strftime

Call .strftime(format) on a datetime. Format codes start with %: %Y is a 4-digit year, %m is the month, and %d is the day.

from datetime import datetime
d = datetime(2026, 5, 30)
print(d.strftime('%Y-%m-%d'))

Formatting the time part

Time codes: %H is hour (00-23), %M is minute, and %S is second. Mix them with literal text however you like.

from datetime import datetime
d = datetime(2026, 5, 30, 9, 7, 3)
print(d.strftime('%H:%M:%S'))

Human-friendly names

Some codes produce names: %A is the full weekday, %B is the full month name, and %b is the abbreviated month.

from datetime import datetime
d = datetime(2026, 5, 30)
print(d.strftime('%A, %B %d, %Y'))

12-hour clocks and AM/PM

Use %I for the 12-hour hour and %p for AM or PM. This is common in user-facing displays.

from datetime import datetime
d = datetime(2026, 5, 30, 14, 30)
print(d.strftime('%I:%M %p'))

Parsing with strptime

datetime.strptime(text, format) reads a string into a datetime. The format must match the text exactly, code for code.

from datetime import datetime
text = '2026-05-30'
d = datetime.strptime(text, '%Y-%m-%d')
print(d)
print(type(d))

Parsing date and time together

Include time codes in the format to parse a full timestamp. Every separator (dashes, spaces, colons) in the text must appear in the pattern too.

from datetime import datetime
text = '30/05/2026 14:05'
d = datetime.strptime(text, '%d/%m/%Y %H:%M')
print(d.year, d.hour, d.minute)

Round trip: parse then format

A common workflow is to parse incoming text, do some logic, then reformat it for output — even into a different layout.

from datetime import datetime
raw = '2026-05-30'
d = datetime.strptime(raw, '%Y-%m-%d')
pretty = d.strftime('%B %d, %Y')
print(pretty)

ISO format shortcut

.isoformat() produces the standard ISO 8601 string, and datetime.fromisoformat() reads it back without needing format codes.

from datetime import datetime
d = datetime(2026, 5, 30, 14, 5)
s = d.isoformat()
print(s)
back = datetime.fromisoformat(s)
print(back == d)

Handling parse errors

If the text does not match the format, strptime raises a ValueError. Wrap it in try/except when input might be malformed.

from datetime import datetime
try:
    datetime.strptime('not-a-date', '%Y-%m-%d')
except ValueError as e:
    print('Could not parse:', e)

Day of year and more

Extra codes: %j is the day of the year (001-366) and %w is the weekday as a number (0=Sunday). These are handy for reports.

from datetime import datetime
d = datetime(2026, 5, 30)
print('Day of year:', d.strftime('%j'))

Quick Check

You have the string '2026-05-30'. Which call converts it into a datetime object?

Recap: Formatting and Parsing

Remember:

  • strftime(fmt) turns a datetime INTO a string.
  • strptime(text, fmt) turns a string INTO a datetime.
  • Common codes: %Y %m %d for dates, %H %M %S for times, %A %B for names.
  • isoformat() and fromisoformat() handle the ISO standard automatically.
from datetime import datetime
d = datetime.strptime('2026-05-30 14:05', '%Y-%m-%d %H:%M')
print(d.strftime('%A at %I:%M %p'))

Frequently asked questions

Is the “Formatting and Parsing” lesson free?

Yes — the full text of “Formatting and Parsing” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Formatting and Parsing”?

Use strftime and strptime. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Formatting and Parsing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. date, time, datetime
  2. Timedelta and Arithmetic
  3. Formatting and Parsing
  4. Time Zones with zoneinfo
← Back to Python Academy