开发者

Python fixed width strings with the Template object

Is there a way make fixed width substitutions with the python String.Template objects substitution language

For example if I wanted s to be 'A string 0003' how would I modify this code:

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

Related开发者_开发百科 question, not using template strings: Setting fixed length with python


read throu PEP 292 , "Simpler String Substitutions" does not make it as expressive as % format. i think you had to format the substitution yourself before feed to the template. something like

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


s=stmpl.substitute(tcount=str(3).zfill(4))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜