0

What are . and : doing in a print statement?

For example:

print(f'The estimated revenue for a $350 film is around ${revenue_estimate:.10}.')

this returns :

The estimated revenue for a $350 film is around $600000000.0.

But without : and . the result is exactly the same!

I searched and found nothing...

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Ali
  • 922
  • 1
  • 9
  • 24
  • [Try to lower the number so see what changes](https://tio.run/##K6gsycjPM/7/vyi1LDWvNDU@tbgkMzexJNVWydDI2MTUzNzCEs5Q4iooyswr0UhTD8lIVYCpTFGA6lVIyy9SSFRQMTY1UEjLzMlVyCxWSCzKL81LUVCpRjffSs@wVk9d8/9/AA "Python 3 – Try It Online"). Tip: [Strig formatting](https://docs.python.org/3/library/string.html#format-specification-mini-language) – 0stone0 Jun 21 '21 at 11:37
  • 2
    Have you [read the docs](https://docs.python.org/3/library/string.html#formatspec)? They explain this syntax in great detail. – Cory Kramer Jun 21 '21 at 11:40
  • .3 - It is the precision operator that sets the precision of the given floating number to 3 places [Source](https://www.programiz.com/python-programming/methods/built-in/format) – mnikley Jun 21 '21 at 11:44
  • Does this answer your question? [How to format a floating number to fixed width in Python](https://stackoverflow.com/questions/8885663/how-to-format-a-floating-number-to-fixed-width-in-python) – Gino Mempin Jan 20 '23 at 12:30

2 Answers2

0

I believe that this notation specifies the type of input. In this case, it is a float cut to 10 decimal places, so writing

revenue_estimate = 4
print(f'The estimated revenue for a $350 film is around ${revenue_estimate:.10}.')

will yield the following:

ValueError                                Traceback (most recent call last)
<ipython-input-61-865ae2076242> in <module>()
      1 revenue_estimate = 4
----> 2 print(f'The estimated revenue for a $350 film is around ${revenue_estimate:.10}.')

ValueError: Precision not allowed in integer format specifier

and 4.01234567894 will be cut to 4.0123456789

user9102437
  • 600
  • 1
  • 10
  • 24
0

Here's your answer:

https://blog.teclado.com/python-formatting-numbers-for-printing/

I was working on the same problem and confused me as well.

After the colon dot, you specify the number of significant digits you want to print. Since your answer of 600000000.0 has exactly 10 significant digits, the whole thing printed. If you changed your code to: print(f'The estimated revenue for a $350 film is around ${revenue_estimate:.9}.'), you would see an output in exponent notation of 6e+08