M.r
M.r

Reputation: 13

String format output

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.

Upvotes: 0

Views: 84

Answers (1)

masnun
masnun

Reputation: 11916

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

Upvotes: 1

Related Questions