11

I found this python example online and I'd like to understand exactly how the number formatting works:

print "%.*g\t%.*g" % (xprecision, a, yprecision, b)

I can see by experimentation that this prints a (with precision xprecision), a tab and then b (with precision yprecision). So, as a simple example, if I run

print "%.*g\t%.*g" % (5, 2.23523523, 3, 12.353262)

then I get

2.2352  12.4

I understand how %g usually works. I also understand how the % generally works. What is confusing me in this example is the construct %.*g. How does the * work here? I can see that it is somehow taking the desired precision value and substituting it into the print expression, but why is that happening? Why does the precision number appear before the number being formatted in (xprecision, a...)?

Can someone break this down and explain it to me?

2 Answers 2

5

* is a size placeholder. It tells the formatting operation to take the next value from the right-hand-side tuple and use that as the precision instead.

In your example, the 'next' value is 5 for the first slot, so you could read this as %.5g, which is used to format 2.23523523. The second slot uses 3 for the width, so becomes %.3g to format 12.353262.

See the String Formatting Operations documenation:

A conversion specifier contains two or more characters and has the following components, which must occur in this order:

(...)

  1. Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.

  2. Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual width is read from the next element of the tuple in values, and the value to convert comes after the precision.

So both the minimum width and the precision can be made variable with *, and the documenation explicitly states that the value to convert comes after the width and precision.

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

3 Comments

Great, thank you! So does this mean that there is no way to specify both the minimum field width and the precision simultaneously?
@EPH no, just use two * characters: %*.*g
Wow, that is cool! Specifying both the field width and the precision will help me keep the columns in my printed files nicely lined up. Thanks!
0

In format specifications, there are field widths with each item. Most of the time it is a constant:

 The value is %.16g

But field widths/precisions can also be variables. The * means replace the splat with the next integer in the format list.

 The value is %.*g

will do the same thing if there is a 16 before the value to format:

 "The value is %.*g" % (16, 14.372492384472)

Comments

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.