Popular recipes by Joonas Paalasmaa http://code.activestate.com/recipes/users/119337/2001-07-12T00:45:34-07:00ActiveState Code RecipesEvaluating code inside strings (Python)
2001-07-12T00:45:34-07:00Joonas Paalasmaahttp://code.activestate.com/recipes/users/119337/http://code.activestate.com/recipes/66018-evaluating-code-inside-strings/
<p style="color: grey">
Python
recipe 66018
by <a href="/recipes/users/119337/">Joonas Paalasmaa</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>In Python, String and Unicode objects have one special operator: the % operator.
With that operator, strings can be formatted with format codes.
Formatting is given syntax format % values, where format is a string with format codes that are replaced with values.
When value is any kind of mapping, formats must include parenthesis that contain a key.
An item is fetched from directory with key or __getitem__ is overloaded with key.
In this example, Eval's __getitem__ returns the result of eval(key).</p>