Get dictionary-keys used to format a string

Fredrik Lundh effbot at
Thu Mar 2 18:43:02 CET 2000

Michael Ströder <michael.stroeder at> wrote:
> Hence the example from the Python docs:
> '%(language)s has %(count)03d quote types.' % vars()
> I would like to extract the keys in the formatting string to a list.
> Well I can parse it myself but is there a built-in function to
> extract a list/tuple of the dictionary keys, e.g.
> ['language','count'] in this case?

>>> s = '%(language)s has %(count)03d quote types.'

>>> import re
>>> re.findall(r'%\((\w+)\)', s)
['language', 'count']

hope this helps!


<!-- (the eff-bot guide to) the standard python library:

More information about the Python-list mailing list