Latest recipes tagged "none" Code Recipesstring Formatter that renders None values as empty strings (Python) 2010-05-14T18:35:27-07:00Antonio Cuni <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[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>