Python `__format__`: Define your own format specification syntax

Python `__format__`: Define your own format specification syntax

Defining a custom __format__ method in Python? Go bananas, if you want. For example (see image for full demo):

def __format__(self, format):
       r = format[0]
       t = format[1:]
       return ''.join(r if c in t else c
                      for c in self.x)