[Python-ideas] String formatting and namedtuple

Antoine Pitrou solipsis at pitrou.net
Fri Feb 13 15:19:52 CET 2009


Guido van Rossum <guido at ...> writes:
> 
> I don't intend to force the issue. I'm disappointed though --
> .format() fixes several common stumbling blocks with %(name)s and at
> least one with %s.

Everytime I try to experiment a bit with format codes, I find them unintuitively
complex:

>>> "{0!r}".format(2.5)
'2.5'
>>> "{0:r}".format(2.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown conversion type r
>>> "{0!f}".format(2.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown conversion specifier f
>>> "{0:f}".format(2.5)
'2.500000'

Why must the 'f' code be preceded by a colon rather than by an exclamation mark?
There is surely a rational explanation, but in day-to-day use it is really
confusing. Add to this the annoyance of typing ".format" and of adding curly
braces everywhere, and "%" is clearly handier despite the lonely tuple problem.

Regards

Antoine.





More information about the Python-ideas mailing list