Suggested coding style

rantingrick rantingrick at gmail.com
Thu Sep 29 19:41:34 EDT 2011


On Sep 29, 6:07 pm, Devin Jeanpierre <jeanpierr... at gmail.com> wrote:
> > However, as you use the new format method you will come to appreciate
> > it. It's an adult beverage with an acquired taste. ;-)
>
> Yeah. It's a much more difficult to read thing, but once you learn how
> to write it it flows faster.
>
> Of course, I never managed to learn how to write it...

A good way to start out is to just use the positional arguments.

py> name = "Bob"

py> "Hello my name is {0}".format(name)
Hello my name is Bob

py> "Hello my name is {name}".format(name=name)
Hello my name is Bob

py> "Hello my name is {0}. My named spelled backwards is:
{0}".format(name)
Hello my name is Bob. My named spelled backwards is: Bob

py> "A small fry cost {0:0.2f}".format(1.6666666)
A small fry cost 1.67

py> "A small car cost {0:,.2f}".format(11666.6666)
A small car cost 11,666.67

# Python 2.7+ you can omit the index.
py> "{} = {}".format("value",2)
value = 2

Start with the small stuff and then diversify! You'll be glad you made
the change.



More information about the Python-list mailing list