1

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

Community
  • 1
  • 1
tjb
  • 10,986
  • 8
  • 64
  • 89

2 Answers2

1

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)
Dyno Fu
  • 8,329
  • 3
  • 36
  • 60
0

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

Ross Patterson
  • 5,533
  • 19
  • 37