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,610
  • 13
  • 81
  • 138

3 Answers3

8
'{value:.2f}'.format(value=pi)
nosklo
  • 217,122
  • 57
  • 293
  • 297
2
>>> pi=3.14159
>>> print('{number:.2f}'.format(number=pi))
3.14
>>>
blhsing
  • 91,368
  • 6
  • 71
  • 106
2

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

MoxieBall
  • 1,907
  • 7
  • 23