4

In Python is possible to combine 2 followings concepts - named arguments and float formatting?

'{0:.2f}'.format(pi)

and

'{first} {last}'.format(first='Hodor', last='Hodor!')

https://stackoverflow.com/a/8940627/2230844

https://pyformat.info/#named_placeholders

denfromufa
  • 5,605
  • 12
  • 71
  • 138

3 Answers3

7
'{value:.2f}'.format(value=pi)
nosklo
  • 205,639
  • 55
  • 286
  • 290
1
>>> pi=3.14159
>>> print('{number:.2f}'.format(number=pi))
3.14
>>>
blhsing
  • 77,832
  • 6
  • 59
  • 90
1

Also worth mentioning is the fstrings answer: print(f'{pi:.2f}')

MoxieBall
  • 1,869
  • 5
  • 21