Printing datetimes in Python is essential for logging, reporting, user interfaces, debugging, API responses, and data exports — turning datetime objects into readable strings using the strftime() method. The method takes a format string with % directives (e.g., %Y for year, %H for hour) and returns a formatted string that can be printed directly or stored. In 2026, consistent datetime printing remains critical — ISO 8601 formats ensure parsability, custom formats improve readability, and vectorized formatting in pandas/Polars scales to large datasets without loops.
Here’s a complete, practical guide to printing datetimes: basic strftime() usage, common format patterns with examples, time zone handling, pandas/Polars vectorization, real-world patterns, and modern best practices for clarity, interoperability, and performance.
The strftime() method is called on datetime (or date) objects — pass a format string to control the output.
from datetime import datetime
now = datetime.now()
# Basic ISO-like datetime
print(now.strftime("%Y-%m-%d %H:%M:%S")) # e.g., 2026-02-10 14:30:45
# Human-readable
print(now.strftime("%A, %B %d, %Y at %I:%M %p")) # e.g., Tuesday, February 10, 2026 at 02:30 PM
# Short numeric
print(now.strftime("%m/%d/%y %H:%M")) # e.g., 02/10/26 14:30
Common format codes for flexible printing:
dt = datetime(2026, 2, 10, 14, 30, 45)
print(dt.strftime("%Y-%m-%dT%H:%M:%S")) # 2026-02-10T14:30:45 (ISO 8601 basic)
print(dt.strftime("%Y-%m-%d %H:%M:%S")) # 2026-02-10 14:30:45
print(dt.strftime("%Y/%m/%d %I:%M:%S %p")) # 2026/02/10 02:30:45 PM
print(dt.strftime("%A %b %d %Y")) # Tuesday Feb 10 2026
print(dt.strftime("%c")) # Tue Feb 10 14:30:45 2026 (locale default)
print(dt.strftime("%x %X")) # 02/10/26 14:30:45 (locale date + time)
Time zone-aware printing — include offset or Z for UTC using %z or %Z.
from zoneinfo import ZoneInfo
utc_now = datetime.now(ZoneInfo("UTC"))
print(utc_now.strftime("%Y-%m-%dT%H:%M:%S%z")) # e.g., 2026-02-10T19:30:45+0000
ny_now = datetime.now(ZoneInfo("America/New_York"))
print(ny_now.strftime("%Y-%m-%d %H:%M:%S %Z")) # e.g., 2026-02-10 14:30:45 EST
Real-world pattern: formatting timestamp columns in pandas — vectorized dt.strftime() turns datetime Series into formatted strings for logs, reports, or exports.
import pandas as pd
df = pd.DataFrame({
'event_time': [datetime(2026, 1, 15, 9, 0), datetime(2026, 2, 10, 14, 30)]
})
# Vectorized formatting
df['iso_timestamp'] = df['event_time'].dt.strftime('%Y-%m-%dT%H:%M:%S')
df['friendly'] = df['event_time'].dt.strftime('%A %I:%M %p, %b %d')
print(df)
# event_time iso_timestamp friendly
# 0 2026-01-15 09:00:00 2026-01-15T09:00:00 Thursday 09:00 AM, Jan 15
# 1 2026-02-10 14:30:00 2026-02-10T14:30:00 Tuesday 02:30 PM, Feb 10
Best practices make datetime printing reliable and performant. Prefer ISO 8601 (%Y-%m-%dT%H:%M:%S) for machine-readable output — always sortable and parsable. Include timezone with %z or %Z in production — avoid naive datetimes. Modern tip: use Polars for large timestamp columns — pl.col("ts").dt.strftime("%Y-%m-%d %H:%M:%S") is 10–100× faster than pandas dt accessor. Add type hints — datetime.datetime — improves readability and mypy checks. Handle time zones with zoneinfo — dt.astimezone(ZoneInfo("UTC")).strftime(...) formats correctly with offset. For microsecond precision, include %f — truncate if not needed. Wrap formatting in try/except — invalid datetimes raise ValueError. Use strftime() with date for date-only, datetime for full timestamp — prevents accidental time inclusion. Combine with pd.to_datetime() or Polars parsing for input ? formatted output pipelines.
Printing datetimes with strftime() gives you complete control over date+time strings — ISO for machines, verbose for humans, custom for reports. In 2026, vectorize in pandas/Polars, include timezone info, use standard codes, and add type hints for safety. Master time-inclusive formatting, and you’ll create consistent, parsable, readable timestamps across logs, APIs, dashboards, and exports.
Next time you need to print or export a datetime — reach for strftime(). It’s Python’s cleanest way to say: “Show this moment exactly how I want it.”