Format strftime is Python’s flexible, powerful method for turning date, time, or datetime objects into human-readable or machine-friendly strings. The strftime() method (string format time) takes a format string with % directives — each representing a specific date/time component — and returns a formatted string. In 2026, strftime() remains the standard for custom date/time formatting — used everywhere from logs, reports, and user interfaces to APIs, CSV/JSON exports, filenames, and database timestamps. It’s fast, locale-aware, and works seamlessly with pandas/Polars for vectorized column formatting.
Here’s a complete, practical guide to strftime(): core format codes, common patterns with examples, real-world use cases, pandas/Polars integration, and modern best practices for readability, localization, timezone handling, and performance.
The method is called on a date or datetime object — pass a format string using % codes for year, month, day, hour, weekday, etc.
from datetime import date, datetime
d = date(2026, 2, 10) # Tuesday, February 10, 2026
dt = datetime(2026, 2, 10, 14, 30, 45) # Same date + 2:30:45 PM
# Basic date formats
print(d.strftime("%Y-%m-%d")) # 2026-02-10 (ISO standard, sortable)
print(d.strftime("%Y/%m/%d")) # 2026/02/10
print(d.strftime("%m/%d/%y")) # 02/10/26 (US style, short year)
print(d.strftime("%B %d, %Y")) # February 10, 2026
print(d.strftime("%A, %B %d, %Y")) # Tuesday, February 10, 2026
Time formats — include hours, minutes, seconds, AM/PM, timezone — use with datetime objects.
print(dt.strftime("%H:%M:%S")) # 14:30:45 (24-hour)
print(dt.strftime("%I:%M %p")) # 02:30 PM (12-hour + AM/PM)
print(dt.strftime("%Y-%m-%d %H:%M:%S")) # 2026-02-10 14:30:45
print(dt.strftime("%c")) # Tue Feb 10 14:30:45 2026 (locale default)
Real-world pattern: formatting date columns in pandas — vectorized dt.strftime() turns timestamp columns into readable strings for reports, exports, or display.
import pandas as pd
df = pd.DataFrame({
'event': [datetime(2026, 1, 15, 9, 0), datetime(2026, 2, 10, 14, 30)]
})
# Vectorized formatting
df['iso_date'] = df['event'].dt.strftime('%Y-%m-%d')
df['full_date'] = df['event'].dt.strftime('%A, %B %d, %Y %I:%M %p')
print(df)
# event iso_date full_date
# 0 2026-01-15 09:00:00 2026-01-15 Thursday, January 15, 2026 09:00 AM
# 1 2026-02-10 14:30:00 2026-02-10 Tuesday, February 10, 2026 02:30 PM
Best practices make strftime() reliable, readable, and performant. Use ISO 8601 (%Y-%m-%d or %Y-%m-%dT%H:%M:%S) for machine-readable output — always sortable and parsable. Prefer isoformat() for standard datetime strings — simpler and timezone-aware if object has tzinfo. Modern tip: use Polars for large timestamp columns — pl.col("ts").dt.strftime("%Y-%m-%d") is 10–100× faster than pandas apply. Add type hints — date or datetime.datetime — improves readability and mypy checks. Handle time zones with zoneinfo — dt.astimezone(ZoneInfo("UTC")).strftime(...) formats correctly with offset. For localization, set locale — import locale; locale.setlocale(locale.LC_TIME, 'fr_FR') — then %A gives French weekday names. Wrap formatting in try/except — invalid dates raise ValueError. Avoid custom format strings unless necessary — standard ISO is interoperable and parsable. Use strftime() with date for date-only, datetime for full timestamp — prevents accidental time inclusion.
strftime() turns dates and times into any string format you need — ISO for machines, human-readable for reports, custom for display. In 2026, use standard codes, vectorize in pandas/Polars, handle time zones, and add type hints for safety. Master strftime(), and you’ll format timestamps cleanly and consistently — readable by users and systems alike.
Next time you need to display or export a date/time — reach for strftime(). It’s Python’s cleanest way to say: “Turn this moment into exactly the string I want.”