Formatting a Python datetime with f-strings and strftime codes

Formatting a Python datetime with f-strings and strftime codes

Have a Python datetime, and want to print it? Use an f-string, and pass a strftime-style format:

f'{now:%Y-%m-%d}’ # year-month-date
f'{now:%Y-%m-%d %H:%M:%S}’ # year-month-date hour:min:sec

# Easily integrate into user output
f’Today is {now:%Y-%m-%d}, you know!’