Is it correct to call it variable expansion? How to embed a numerical value etc. in a character string variable.
ruby
name = "World"
month = 9
day = 9
print "Hellow #{name}!,today#{month}/#{day}is"
It's the one you want to see.
As a result of investigation
name = "World"
month = 9
day = 9
print u"Hellow {name}!,today{month}/{day}is".format(**locals())
It seems to write. .format (** locals ())
will quickly get annoying, and if you use it continuously
Because it looks quite noisy
f = lambda x, l=locals(): x.format(**l)
print f(u"Hellow {name}!,today{month}/{day}is")
Will you do this?
The idea is to deploy it using Jinja.
>>> from jinja2 import Template
>>> fj = lambda str, dict=locals(): Template(str).render(dict)
>>> name = "World"
>>> month = 9
>>> day = 9
>>> print fj(u"Hellow {{name}}!,today{{month}}/{{day}}is.")
Hellow World!,9 today/It is 9.
>>> print fj(u"tomorrow{{month}}/{{day+1}}is.")
9 tomorrow/It is 10.
It is like this. I don't know much about how to use Jinja, but I can calculate a little, so this may be good.
http://stackoverflow.com/questions/19549980/short-form-for-string-format-locals
Recommended Posts