string formatting

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri May 6 04:46:04 EDT 2011


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?


[steve at sylar ~]$ python3.2
Python 3.2a1 (r32a1:83318, Aug 12 2010, 02:17:22)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import warnings
>>> warnings.simplefilter("always")
>>> "%d" % 42
'42'


I see no DeprecationWarning.



-- 
Steven



More information about the Python-list mailing list