What does the colon and the dot in a print statement mean?

Question:

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…

Asked By: Ali

||

Answers:

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

Answered By: user9102437

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

Answered By: florida_man1775
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.