[Tutor] What are these two string-formatting styles called?

Dave Angel davea at davea.name
Sat Jun 15 13:32:12 CEST 2013


On 06/15/2013 06:23 AM, Dotan Cohen wrote:
> On Fri, Jun 14, 2013 at 7:01 PM, Jim Mooney <cybervigilante at gmail.com> wrote:
>> On 14 June 2013 08:23, Dotan Cohen <dotancohen at gmail.com> wrote:
>>>
>>> What are these two string-formatting styles called?
>>> '%.3f' % x
>>> '{0:.3f}'.format(x)
>>
>>
>> The first one is a string Expression, using % as the overloaded operator
>> The second one is a string method, with .format() as the method for a string
>> object
>>
>
> Thank you. So would it be clear if I were to say "I prefer
> printf-style formatting over the format method."?
>

I'd be careful there, since method is an English word as well as a 
Python one.  So I'd make it clear i was referrring to a method of a 
class, by naming the class.

Something like:

the format() method of the str class.



-- 
DaveA


More information about the Tutor mailing list