-2

How can I format currency using python template strings? My goal is to generate the following string:

'Hello Johnny. It is $10'

I've tried the following:

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'
Johnny Metz
  • 5,977
  • 18
  • 82
  • 146

1 Answers1

0

$$ escapes the dollar sign so three dollar signs are required:

Template('Hello ${name}. It is $$$cost').substitute(d)
# 'Hello Johnny. It is $10'
Johnny Metz
  • 5,977
  • 18
  • 82
  • 146