2

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Your format string is 01.2f, and originally I had 1.2f, so I changed it to match yours.
oh, What does zero padding do? I copied that code from somewhere.
Oh, it doesn't seem to work though. The above formatting outputs in this format: 9.55, rather than 09.55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.