String formatting with datetime
suggest changeAny 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
>>> dt = datetime(2016, 7, 21)
>>> 'North America: {dt:%m/%d/%Y}. ISO: {dt:%Y-%m-%d}.'.format(dt=dt)
'North America: 07/21/2016. ISO: 2016-07-21.'
datetime.now() would use the current date instead of this fixed one. A full list of datetime formatters can be found in the official documentation.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents