1

How to use Python3 new feature : "f-string" to output the first 50 bits of math.pi?

We can achieve it by the following 2 old ways: 1 ("%.50f"% math.pi) 2 '{.50f}'.format(math.pi) But for the new feature "f-string",I knew that we can use this format: f"the value of pi is {math.pi}", but how to limit and filter the first 50 bits?

In [2]: ("%.50f"%math.pi)
Out[2]: '3.14159265358979311599796346854418516159057617187500'
Netwave
  • 40,134
  • 6
  • 50
  • 93
huoke
  • 11
  • 1
  • Possible duplicate of [Fixed digits after decimal with f-strings](https://stackoverflow.com/questions/45310254/fixed-digits-after-decimal-with-f-strings) – Proyag May 27 '19 at 15:41

1 Answers1

0

Same formatting as with str.format, but using the variable name in the first block before the ::

>>> import math
>>> f"{math.pi:.50f}"
'3.14159265358979311599796346854418516159057617187500'
>>> f"the value of pi is {math.pi:.50f}"
'the value of pi is 3.14159265358979311599796346854418516159057617187500'
Netwave
  • 40,134
  • 6
  • 50
  • 93