String Formatting with new .format()

Frank Millman frank at chagford.com
Wed Mar 28 10:49:17 EDT 2018


> "Ganesh Pal"  wrote in message 
> news:CACT3xuUmOzR=5G9=zAf3fp2LYtbGJv74VSyJfsVsiFO77LFcjQ at 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





More information about the Python-list mailing list