String Formatting with new .format()

W Yg ygguang1992 at gmail.com
Mon Mar 26 20:13:50 EDT 2018


在 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)



More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /