0

Having a source program like below:

from string import Template
s=Template('$x,glorious $x!')
print s.substitute(x='slurm')
print s
print Template

and Output like below:

slurm,glorious slurm!
<string.Template object at 0x024955D0>
<class'string.Template'>

Why?The last outputs I can't understand.

3
  • 3
    because you are printing a string.Template instance and then just the class Template ... Commented Dec 9, 2015 at 6:50
  • Thanks.I make a mistake ,I think s.substitute will change original array.lol Commented Dec 9, 2015 at 7:08
  • 1
    @M.r No, substitute doesn't change the original Template, it returns a new string rendered from the template. Commented Dec 9, 2015 at 7:41

1 Answer 1

1

Here you are creating a template:

s=Template('$x,glorious $x!')

Now you are parsing the template, replacing x with slurm:

print s.substitute(x='slurm')

Please notice, the above call returns a new string which is what you want to store in a new variable if you need to use it somewhere else.

You are printing s which is a Template object:

print s

You are printing Template which is a class you imported:

print Template

So probably this is what you wanted:

from string import Template
s=Template('$x,glorious $x!')
result = s.substitute(x='slurm')
print result
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.