views:

353

answers:

1

How can I format a string like this

"%01.2f" % pr"%01.2f" % some_number

but using the str.format() style? I'm used to this:

'{0} {1}'.format(somevalue1, somevalue2)

Is there a way to use that function style vs the old % style, and still achieve this decimal formatting?

+3  A: 

It would be like {0:01.2f}, I believe. See http://docs.python.org/library/string.html#format-string-syntax.

Edit: added in your zero-padding.

danben
I checked in a Python interpreter, and that's right.
LeafStorm
What's the zero-padding for?
orokusaki
Your format string is 01.2f, and originally I had 1.2f, so I changed it to match yours.
danben
oh, What does zero padding do? I copied that code from somewhere.
orokusaki
It means that 5 will show up as 05, for example.
danben
Oh, it doesn't seem to work though. The above formatting outputs in this format: 9.55, rather than 09.55
orokusaki