.format vs. %

Robert Kern robert.kern at gmail.com
Sat Dec 31 14:43:50 EST 2011


On 12/31/11 7:34 PM, Lie Ryan wrote:
> On 01/01/2012 05:44 AM, davidfx wrote:
>> Thanks for your response. I know the following code is not going to be correct
>> but I want to show you what I was thinking.
>>
>> formatter = "%r %r %r %r"
>>
>> print formatter % (1, 2, 3, 4)
>>
>> What is the .format version of this concept?
>>
>
> I don't think the (%r)epr-formatting exist anymore, so if you want to do that
> you'll need to call repr manually.

Yes, it does.

formatter = '{!r} {!r} {!r} {!r}'
print formatter.format(1,2,3,4)

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list