Let's talk a bit …
Oct 14th
Percent in python’s string formatting
In python, we don’t need to escape the char ‘%’ in a string, except when we want to format the specific string. Examples:
>>> 'Here is a percent: %' 'Here is a percent: %' >>> 'Here is two percent %%' 'Here is two percent %%' >>> 'Here is a percent %% %s' % 'in formatted string' 'Here is a percent % in formatted string' >>> 'We have to double the percent % %s!' % 'in formatted string' Traceback (most recent call last): File "", line 1, in TypeError: not all arguments converted during string formatting >>> 'If we do not specify a format, we must not double the percent: %(foo)s %(bar)s!' 'If we do not specify a format, we must not double the percent: %(foo)s %(bar)s!' >>> 'If we want to format specifically, we must double the percent: %(foo)s %%(bar)s!' % {'foo': 'test'} 'If we want to format specifically, we must double the percent: test %(bar)s!' >>> 'And of course this will crash: %(foo)s %(bar)s!' % {'foo': 'test'} Traceback (most recent call last): File " ", line 1, in KeyError: 'bar'