Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python string formatting - combine named placeholder and float formatter for the same argument?

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

like image 967
denfromufa Avatar asked Jan 02 '23 05:01

denfromufa


2 Answers

'{value:.2f}'.format(value=pi)
like image 170
nosklo Avatar answered Jan 05 '23 18:01

nosklo


>>> pi=3.14159
>>> print('{number:.2f}'.format(number=pi))
3.14
>>>
like image 20
blhsing Avatar answered Jan 05 '23 16:01

blhsing