1
#!/usr/bin/python

from string import Template

s = Template('$x, go home $x')
s.substitute(x='lee')

print s

error i get is

<string.Template object at 0x81abdcc>

desired results i am looking for is : lee, go home lee

3 Answers 3

7

You need to look at the return value of substitute. It gives you the string with substitutions performed.

print s.substitute(x='lee')

The template object itself (s) is not changed. This gives you the ability to perform multiple substitutions with the same template object.

Sign up to request clarification or add additional context in comments.

Comments

3

You're not getting an error: you're getting exactly what you're asking for -- the template itself. To achieve your desired result,

print s.substitute(x='lee')

Templates, like strings, are not mutable objects: any method you call on a template (or string) can never alter that template -- it can only produce a separate result which you can use. This, of course, applies to the .substitute method. You're calling it, but ignoring the result, and then printing the template -- no doubt you expect the template itself to be somehow altered, but that's just not how it works.

Comments

1
print s.substitute(x='lee')

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.