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

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.