Python使用模板对象固定宽度字符串

Python使用模板对象固定宽度字符串,python,Python,有没有办法用python String.Template对象替换语言进行固定宽度的替换 例如,如果我希望s成为“字符串0003”,我将如何修改此代码: from string import Template stmpl=Template("A string ${tcount}") s=stmpl.substitute(tcount=3) print(s) 相关问题,不使用模板字符串: 通过阅读,“更简单的字符串替换”并不像%格式那样具有表现力。我认为在向模板馈送之前,您必须自己格式化替换。差不

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

例如,如果我希望s成为“字符串0003”,我将如何修改此代码:

from string import Template
stmpl=Template("A string ${tcount}")
s=stmpl.substitute(tcount=3)
print(s)
相关问题,不使用模板字符串: 通过阅读,“更简单的字符串替换”并不像%格式那样具有表现力。我认为在向模板馈送之前,您必须自己格式化替换。差不多

"{0:05}".format(3)
通过阅读,“更简单的字符串替换”并不像%格式那样具有表现力。我认为在向模板馈送之前,您必须自己格式化替换。差不多

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