-2

Python テンプレート文字列を使用して通貨をフォーマットするにはどうすればよいですか? 私の目標は、次の文字列を生成することです。

'Hello Johnny. It is $10'

私は次のことを試しました:

from string import Template
d = {'name': 'Johnny', 'cost': 10}

Template('Hello ${name}. It is ${cost}').substitute(d)
# 'Hello Johnny. It is 10'

Template('Hello ${name}. It is $${cost}').substitute(d)
# 'Hello Johnny. It is ${cost}'

Template('Hello ${name}. It is $$cost').substitute(d)
# 'Hello Johnny. It is $cost'
4

1 に答える 1