Extract keys from Python's format strings

Sometimes in Python you may need to diagnose some format strings. Maybe they are supposed to be provided by the user and you need to know which keys to fill it with. Another prominent example is translation software: format string placeholders are definitely a thing there, and you may want to do something special about them.

First, let's get the old-style format strings out of the way. I haven't seen any introspection capabilities for them in the standard library, so instead this neat workaround can be used: a format string is formatted by a dict-like object, which really just stores every key that it gets asked for.

fmt_string = "My name is %(name)s. I am %(age)s years old."

class AccessSaver:
    def __init__(self):
        self.keys = []
    def __getitem__(self, key):

a = AccessSaver()
fmt_string % a

#< ['name', 'age']

If a format string with positional arguments is supplied here, TypeError will be raised by the formatting operator.

For new-style format strings the exact same class can be used, together with str.format_map in Python 3.2+.

But there is a much better way: string.Formatter.parse:

fmt_string = "My name is {name}. I am {age} years old."

import string

keys = [t[1] for t in string.Formatter().parse(fmt_string) if t[1] is not None]
#< ['name', 'age']

Format spec is ignored here, but feel free to extract it as well.

Created (last updated )
Comments powered by Disqus