Custom Formatters
Explore how to apply Python's custom string formatting capabilities using the format() method and f-strings. Understand how to use positional and named placeholders in templates and consider extending formatting via overriding __format__. This lesson helps you manipulate strings effectively in applications.
We'll cover the following...
Overview
While these standard formatters apply to most built-in objects, it is also possible for other objects to define nonstandard specifiers. For example, if we pass a datetime object into format, we can use the specifiers used in the datetime.strftime() function, as follows:
It is even possible to write custom formatters for objects we create ourselves, but that is beyond the scope of this book. Look into overriding the __format__() special method if we need to do this in our code.
The Python formatting syntax is quite flexible, ...