Turning dates into strings is a core task when working with dates in Python — whether for logging, reporting, displaying to users, exporting to CSV/JSON, or formatting timestamps for APIs. The strftime() method (string format time) on date or datetime objects converts dates to strings using flexible format codes like %Y (year), %m (month), %d (day), %A (weekday name), and many more. In 2026, mastering strftime() remains essential — it’s fast, locale-aware (with proper setup), and works seamlessly with pandas/Polars for vectorized formatting of large date columns.
Here’s a complete, practical guide to turning dates into strings: how strftime() works, common format codes, real-world patterns, pandas/Polars integration, and modern best practices for readability, localization, and performance.
The strftime() method takes a format string and returns the date/time as formatted text — codes start with % and represent specific parts of the date/time.
from datetime import date, datetime
d = date(2026, 2, 10) # February 10, 2026 (Tuesday)
# Basic ISO date
print(d.strftime("%Y-%m-%d")) # 2026-02-10
# Full weekday name + month + day + year
print(d.strftime("%A, %B %d, %Y")) # Tuesday, February 10, 2026
# Short month, day, two-digit year
print(d.strftime("%b %d, %y")) # Feb 10, 26
# Numeric month/day/year with slashes
print(d.strftime("%m/%d/%Y")) # 02/10/2026
For datetime objects, include time codes — %H (24-hour), %M (minute), %S (second), %p (AM/PM), etc.
dt = datetime(2026, 2, 10, 14, 30, 45)
print(dt.strftime("%Y-%m-%d %H:%M:%S")) # 2026-02-10 14:30:45
print(dt.strftime("%I:%M %p on %A")) # 02:30 PM on Tuesday
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_date': [date(2026, 1, 15), date(2026, 2, 10), date(2026, 3, 5)]
})
# Vectorized formatting
df['formatted_date'] = df['event_date'].apply(lambda d: d.strftime("%A, %b %d, %Y"))
df['iso_date'] = df['event_date'].apply(lambda d: d.strftime("%Y-%m-%d"))
print(df)
# event_date formatted_date iso_date
# 0 2026-01-15 Thursday, Jan 15, 2026 2026-01-15
# 1 2026-02-10 Tuesday, Feb 10, 2026 2026-02-10
# 2 2026-03-05 Thursday, Mar 05, 2026 2026-03-05
Best practices make date-to-string conversion safe, readable, and performant. Use standard format codes — %Y-%m-%d for ISO (universal), %Y-%m-%d %H:%M:%S for timestamps. Prefer strftime() over manual string building — it’s faster and handles edge cases (leading zeros, locale). Modern tip: use Polars for large date columns — pl.col("date").dt.strftime("%Y-%m-%d") is 10–100× faster than pandas apply. Add type hints — date or datetime.date — improves readability and mypy checks. Handle invalid dates — wrap date() creation in try/except ValueError. Use strftime() with locale — import locale; locale.setlocale(locale.LC_TIME, 'fr_FR') for French month names. For timezone-aware datetimes, use strftime() with ZoneInfo — formats correctly with offset. Avoid custom format strings unless necessary — standard ISO is interoperable and parsable.
Turning dates into strings with strftime() gives you full control over formatting — ISO for machines, human-readable for reports. In 2026, vectorize in pandas/Polars, use standard codes, add type hints, and handle locales/timezones properly. Master date formatting, and you’ll display, export, and log dates cleanly and consistently — readable by users and systems alike.
Next time you need to turn a date into a string — reach for strftime(). It’s Python’s cleanest way to say: “Format this date exactly how I want it.”