Hervé Cauwelier added the comment:
Thanks for the example. The Python 2.7 documentation about the mini-language
doesn't clearly state that it is extensible and how. we see examples of
formatting but not of extending.
Your example would be welcome in the documentation.
--
___
Eric Smith added the comment:
I agree with David.
Here's an example of using such a subclass. It extends the format string for
strings to begin with an optional 'u' or 'l':
---
class U(str):
def __format__(self, fmt):
if fmt[0] == 'u':
s = self.upper
R. David Murray added the comment:
The format support is written specifically so that it is extensible. You can
write your own string subclass that extends the formatting mini-language with
whatever features you find useful. There are too many variations on what might
be useful with regards
New submission from Hervé Cauwelier :
Hexadecimals can be formatted to lower and uppercase:
>>> '{0:x}'.format(123)
'7b'
>>> '{0:X}'.format(123)
'7B'
I would like the same thing for strings:
>>> '{0.lastname:u} {0.firstname}'.format(user)
'DOE John'
I first thought using "S" for uppercase, bu