Printing Datetimes in Python – Best Practices for Data Science 2026
Printing datetime objects clearly and consistently is essential for logging, debugging, reports, dashboards, and API responses. In data science, you need both machine-readable formats (for storage and APIs) and human-readable formats (for logs and reports). Python gives you several clean ways to control exactly how datetimes appear when printed.
TL;DR — Recommended Printing Methods
print(dt)orstr(dt)→ default ISO-like formatdt.strftime(...)→ full control over formatdt.isoformat()→ standard machine-readable output- pandas
.dt.strftime()→ vectorized printing for DataFrames
1. Basic Printing of Datetimes
from datetime import datetime
from zoneinfo import ZoneInfo
now = datetime.now(ZoneInfo("UTC"))
print(now) # Default: 2026-03-19 14:30:25.123456+00:00
print(str(now)) # Same as above
print(now.isoformat()) # Clean ISO 8601: 2026-03-19T14:30:25.123456+00:00
2. Custom Formatting with strftime()
print(now.strftime("%Y-%m-%d %H:%M:%S")) # 2026-03-19 14:30:25
print(now.strftime("%A, %B %d, %Y at %I:%M %p")) # Thursday, March 19, 2026 at 02:30 PM
print(now.strftime("%d/%m/%Y %H:%M")) # 19/03/2026 14:30
3. Real-World Data Science Examples
import pandas as pd
df = pd.read_csv("sales_data.csv", parse_dates=["order_date"])
# Add nicely formatted columns for reports
df["order_date_human"] = df["order_date"].dt.strftime("%A, %B %d, %Y")
df["order_time_human"] = df["order_date"].dt.strftime("%I:%M %p")
# For logging
for row in df.itertuples():
print(f"Order at {row.order_date.strftime('%Y-%m-%d %H:%M:%S')} by customer {row.customer_id}")
4. Best Practices in 2026
- Use
strftime()for human-readable output in logs and reports - Use
isoformat()for machine-readable output (APIs, JSON, storage) - Always print timezone-aware datetimes with their offset
- Use pandas
.dt.strftime()for bulk formatting in DataFrames - Define common format strings as constants for consistency across your project
Conclusion
Printing datetimes effectively is a small but important skill that improves the readability of logs, reports, and dashboards. In 2026, combine strftime() for beautiful human-readable output and isoformat() for clean machine-readable output. These techniques make your data science code more professional and easier to work with for both humans and machines.
Next steps:
- Add formatted datetime columns to your current datasets using
strftime()for better reporting and logging