string formatting

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri May 6 10:24:56 EDT 2011


On Fri, 06 May 2011 02:23:19 -0700, Chris Rebert wrote:

> On Fri, May 6, 2011 at 1:46 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> On Fri, 06 May 2011 10:00:30 +0200, Web Dreamer wrote:
>>> Jabba Laci a écrit ce vendredi 6 mai 2011 09:18 dans
>>> <mailman.1229.1304666321.9059.python-list at python.org> :
>>>> Hi,
>>>>
>>>> Which is the preferred way of string formatting?
>>>>
>>>> (1) "the %s is %s" % ('sky', 'blue')
>>>>
>>>> (2) "the {0} is {1}".format('sky', 'blue')
>>>>
>>>> (3) "the {} is {}".format('sky', 'blue')
>>>>
>>>> As I know (1) is old style. (2) and (3) are new but (3) is only
>>>> supported from Python 2.7+.
>>>>
>>>> Which one should be used?
>>>
>>> According to python's documentation, (3) and (2) are preferred and (1)
>>> is deprecated.
>>
>> I think you are wrong, % formatting is not deprecated. Do you have a
>> link showing otherwise?
> 
> I'm not them, but:
> "Note: The formatting operations described here [involving %] are
> obsolete and may go away in future versions of Python. Use the new
> String Formatting [i.e. format()] in new code."
> http://docs.python.org/dev/library/stdtypes.html#old-string-formatting-
operations
> 
> Technically, not formally deprecated, but such a characterization isn't
> too far off the mark either.

Not deprecated at all. In context, deprecation *is* a formal process.

In any case, on occasions that the issue has been raised, there has been 
considerable community objection to removal of % formatting. Guido might 
want to remove it, but I wouldn't bet on it happening before Python 4000.

It's perfectly safe to continue using % formatting, if you choose.



-- 
Steven



More information about the Python-list mailing list