Преобразование в строку

Простые типы / Дата и время

from datetime import datetime

# A fixed moment: the output stays the same
moment = datetime(2027, 1, 1, 22, 13, 20)

# %I is the 12-hour clock, %p is AM/PM
short_style = moment.strftime("%m/%d/%y %I:%M %p")
# short_style is '01/01/27 10:13 PM'

custom_style = moment.strftime("%Y-%m-%d")
# custom_style is '2027-01-01'

# No format: the date, a space, the time
standard_style = str(moment)
# standard_style is '2027-01-01 22:13:20'

# ISO 8601: "T" between date and time
iso_style = moment.isoformat()
# iso_style is '2027-01-01T22:13:20'

# An f-string takes the same codes
str_format = f"Today {moment:%d %B}"
# str_format is 'Today 01 January'

print(f"{short_style = }")
print(f"{custom_style = }")
print(f"{standard_style = }")
print(f"{iso_style = }")
print(f"{str_format = }")