Partial substitution similar to str % dict

Emile van Sebille emile at
Sun Aug 18 13:10:46 EDT 2002

Stefan Schwarzer
> I would like to do "partial" string substitutions, for example
>  >>> partial_substitute( "%(foo)s-%(bar)s", {'foo': 'abc'} )
> 'abc-%(bar)s'

class Partial(dict):
    def __getitem__(self, ky):
                    return self.__dict__[ky]
            except KeyError:
                return '%%(%s)' % ky

print "%(foo)s-%(bar)s" % Partial({'foo': 'abc'})



Emile van Sebille
emile at


More information about the Python-list mailing list