【问题标题】:Python fixed width strings with the Template object带有模板对象的 Python 固定宽度字符串
【发布时间】:2011-08-20 06:11:26
【问题描述】:

有没有办法使用 python String.Template 对象替换语言进行固定宽度替换

例如,如果我希望 s 为 'A string 0003',我将如何修改此代码:

from string import Template
stmpl=Template("A string ${tcount}")
s=stmpl.substitute(tcount=3)
print(s)

相关问题,不使用模板字符串: Setting fixed length with python

【问题讨论】:

    标签: python


    【解决方案1】:

    通读PEP 292,“更简单的字符串替换”并不像 % 格式那样具有表现力。我认为您必须在输入模板之前自己格式化替换。像

    "{0:05}".format(3)
    

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      相关资源
      最近更新 更多