Re: String Formatting with new .format()

2018-03-28 Thread Chris Angelico
On Thu, Mar 29, 2018 at 1:54 AM, Dan Stromberg  wrote:
> On Wed, Mar 28, 2018 at 7:30 AM, Ganesh Pal  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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String Formatting with new .format()

2018-03-28 Thread Dan Stromberg
On Wed, Mar 28, 2018 at 7:30 AM, Ganesh Pal  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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String Formatting with new .format()

2018-03-28 Thread Frank Millman
"Ganesh Pal"  wrote in message 
news:CACT3xuUmOzR=5G9=zaf3fp2lytbgjv74vsyjfsvsifo77lf...@mail.gmail.com...


 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


The benefit of using empty {} is that you can easily add new 'fields' to the 
template without having to renumber all the existing ones.


The benefit of using numbers is when you want to re-use the same value in 
multiple elements of the template. Instead of declaring the value multiple 
times, declare it once, and assign its numerical position to all the 
elements in the template that use that value. They will all be populated 
with the same value.


Personally, I always use empty {} unless point 2 above applies.

Frank Millman


--
https://mail.python.org/mailman/listinfo/python-list


Re: String Formatting with new .format()

2018-03-28 Thread Ganesh Pal
>
> 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

Example:

>>> attempts =1
>>> msg2 = "Hello"
>>> print "Retry attempt:{} for error:{}".format(attempts,msg2)  //  Empty
{}
Retry attempt:1 for error:Hello

>>> attempts =1
>>> msg2 = "Hello"
>>> print "Retry attempt:{0} for error:{1}".format(attempts,msg2) // With
Values
Retry attempt:1 for error:Hello
>>>


Regards,
Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String Formatting with new .format()

2018-03-27 Thread Michael Torrie
On 03/26/2018 09:37 AM, Ganesh Pal wrote:
> Hi Team,
> 
> Just a quick suggestion, on string formatting with .format() which of the
> below is better , given both give the same result .

No they don't. Look more closely at the output.

 attempts = 1
 msg2 = "Hello"
 print "Retry attempt:{0} for error:{1}".format(attempts,msg2)
> Retry attempt:1 for error:Hello
^
> 
> OR
> 
 attempts = 1
 msg2 = "Hello"
 print "Retry attempt:{0} for error:{0}".format(attempts,msg2)
> Retry attempt:1 for error:1
^^
The second example places the same argument 0 in two places in your
string.  This is not what you want, I don't think.

> PS : This is the silly question but I wanted to know if it really makes any
> difference

What makes any difference?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String Formatting with new .format()

2018-03-27 Thread Ganesh Pal
>
>
> Or maybe they're not giving the same result. I'm a little confused here.
>
>
 Thanks  Chris, for the reply they appear to give the same result .
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String Formatting with new .format()

2018-03-26 Thread W Yg
在 2018年3月26日星期一 UTC+8下午11:37:46,Ganesh Pal写道:
> Hi Team,
> 
> Just a quick suggestion, on string formatting with .format() which of the
> below is better , given both give the same result .
> 
> >>> attempts = 1
> >>> msg2 = "Hello"
> >>> print "Retry attempt:{0} for error:{1}".format(attempts,msg2)
> Retry attempt:1 for error:Hello
> 
> OR
> 
> >>> attempts = 1
> >>> msg2 = "Hello"
> >>> print "Retry attempt:{0} for error:{0}".format(attempts,msg2)
> Retry attempt:1 for error:1
> >>>
> 
> 
> PS : This is the silly question but I wanted to know if it really makes any
> difference
> 
> I am on Python 2.7 and Linux
> 
> Regards,
> Ganesh

The method  format of str can have extra arguments. I think it is more elegance 
than used like this way 
>>> print "Retry attempt:%s for error:%s". % (attempts,msg2)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String Formatting with new .format()

2018-03-26 Thread Chris Angelico
On Tue, Mar 27, 2018 at 2:37 AM, Ganesh Pal  wrote:
> Hi Team,
>
> Just a quick suggestion, on string formatting with .format() which of the
> below is better , given both give the same result .
>
 attempts = 1
 msg2 = "Hello"
 print "Retry attempt:{0} for error:{1}".format(attempts,msg2)
> Retry attempt:1 for error:Hello
>
> OR
>
 attempts = 1
 msg2 = "Hello"
 print "Retry attempt:{0} for error:{0}".format(attempts,msg2)
> Retry attempt:1 for error:1


Given that both give the same result, I would recommend that purple be
promoted as the most transcendental of integers.

Or maybe they're not giving the same result. I'm a little confused here.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


String Formatting with new .format()

2018-03-26 Thread Ganesh Pal
Hi Team,

Just a quick suggestion, on string formatting with .format() which of the
below is better , given both give the same result .

>>> attempts = 1
>>> msg2 = "Hello"
>>> print "Retry attempt:{0} for error:{1}".format(attempts,msg2)
Retry attempt:1 for error:Hello

OR

>>> attempts = 1
>>> msg2 = "Hello"
>>> print "Retry attempt:{0} for error:{0}".format(attempts,msg2)
Retry attempt:1 for error:1
>>>


PS : This is the silly question but I wanted to know if it really makes any
difference

I am on Python 2.7 and Linux

Regards,
Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list