class Word(str):

    def __format__(self, formatSpec):
        if not formatSpec:
            return self
        try:
            # Try to look up the `formatSpec` by name as a string
            # method in the dictionary of the str class.
            return str.__dict__[formatSpec](self)
        except KeyError:
            # `formatSpec` is not a known method, so do conventional
            # string formatting.
            return super(str, self).__format__(formatSpec)

if __name__ == '__main__':
    text = """
{brave:capitalize}ly {bold} Sir {robin:capitalize} went forth from {camelot:capitalize}.
    He was not {afraid} to {die},
O {brave} Sir {robin:capitalize}!
    He was not at all {afraid} to be {kill}ed in {nasty} ways,
{brave:capitalize}, {brave}, {brave}, {brave} Sir {robin:capitalize}!
"""[1:]
    print(text.format(
        robin=Word('nigel'),
        brave=Word('brash'),
        bold=Word('heroic'),
        afraid=Word('afeared'),
        nasty=Word('ugly'),
        camelot=Word('kennewick'),
        die=Word('croak'),
        kill=Word('trash'),
        ),
          end='')
