Python Language String formatting with datetime


Any class can configure its own string formatting syntax through the __format__ method. A type in the standard Python library that makes handy use of this is the datetime type, where one can use strftime-like formatting codes directly within str.format:

>>> from datetime import datetime
>>> 'North America: {dt:%m/%d/%Y}.  ISO: {dt:%Y-%m-%d}.'.format(
'North America: 07/21/2016.  ISO: 2016-07-21.'

A full list of list of datetime formatters can be found in the official documenttion.