Top-rated recipes tagged "none"http://code.activestate.com/recipes/tags/none/top/2010-05-14T18:35:27-07:00ActiveState Code Recipesstring Formatter that renders None values as empty strings (Python)
2010-05-14T18:35:27-07:00Antonio Cunihttp://code.activestate.com/recipes/users/4169762/http://code.activestate.com/recipes/577227-string-formatter-that-renders-none-values-as-empty/
<p style="color: grey">
Python
recipe 577227
by <a href="/recipes/users/4169762/">Antonio Cuni</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/none/">none</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>This string Formatter works exactly as the default string.Formatter one (i.e., as str.format), with the exception that None values are rendered as empty strings instead of "None". Moreover, any attempt to access attributes or items of None values is rendered as an empty string as well, instead of raising an exception. <br />
E.g. fmt.format('{a}{a.foo}{a[0]}', a=None) == ''</p>
<p>This is useful e.g. when filling a template with values that are fetched from a DB, where usually None means empty.</p>