开发者

Python fixed width strings with the Template object

开发者 https://www.devze.com 2023-03-29 16:47 出处:网络
Is there a way make fixed width substitutions with the python String.Template objects substitution language

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

精彩评论

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