String Formatting with new .format()

Chris Angelico rosuav at gmail.com
Wed Mar 28 12:07:19 EDT 2018


On Thu, Mar 29, 2018 at 1:54 AM, Dan Stromberg <drsalists at gmail.com> wrote:
> On Wed, Mar 28, 2018 at 7:30 AM, Ganesh Pal <ganesh1pal at gmail.com> wrote:
>>>
>>> Or maybe they're not giving the same result. I'm a little confused here.
>>>
>>
>>
>> My Bad and Apologies  , I should be fined for pasting wrong question.
>>
>>  Actually I wanted to know if its ok to use just empty {}  with .format()
>> or  use {} with values i.e {0} {1} both will give the same results anyway
>
> 2.6 and 3.0 do not like {}, but do like {0}.
>
> 2.7 and 3.1+ are fine with either.

Fairly weak argument there :)

More notably, though: {0} {1} gives you stability, but {} {} gives you
simplicity. Use whichever one makes more sense. And if you're creating
localizable messages (where the template goes into a separate file and
the formatting happens a long way separate from it), you can take
flexibility even further by using {spam} {ham} and keyword arguments -
extremely helpful when you're reading through the message file.

ChrisA



More information about the Python-list mailing list