【问题标题】:python : template var without spacepython:没有空格的模板var
【发布时间】:2013-04-18 16:04:58
【问题描述】:

在python中,我尝试使用模板

 from string import Template
 s = Template("hello $world")
 print s.substitute(world="Stackoverflow")

好的,但是,在模板中我需要连接一个没有空格的字符串,例如

 s = Template("a small number : $number e6")
 print s.substitute(number=5)

我需要a small number : 5e6 作为输出,5 和 e6 之间没有空格。

【问题讨论】:

    标签: python templates


    【解决方案1】:

    在模式周围使用花括号:

    Template("a small number : ${number}e6")
    

    结果:

    >>> Template("a small number : ${number}e6").substitute(number=5)
    'a small number : 5e6'
    

    【讨论】:

      猜你喜欢
      • 2014-06-17
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 2014-01-21
      • 2010-12-26
      • 2016-04-10
      • 2016-10-16
      相关资源
      最近更新 更多