strftime()

strftime turns a date into text using format codes such as %Y-%m-%d; strptime does the reverse, parsing text into a datetime.

In more detail

The f in strftime is for format and the p in strptime for parse. The format codes must match the text exactly for strptime, or it raises ValueError.

Example

d.strftime("%d %B %Y")
datetime.strptime("2026-09-29", "%Y-%m-%d")

Related terms

Taught in

References