954,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Storing a Template String?

Basically I'm trying to take a Template String and store it in a variable for later use. I'm new with Python and programming languages in general so I'm finding difficulty in doing this. This is the code I'm using to generate the string:

from string import Template
s = Template('$who has $amount dollars.')
s.substitute(who='Merideth', amount='65')



Fairly simple, but how do I take that all and store it in a variable? I'd like to store the string in a variable and print it later.

Thanks in advance. Hate to bother with a beginner question, but we all have to start somewhere...

AikoYamamato
Newbie Poster
2 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Basically I'm trying to take a Template String and store it in a variable for later use. I'm new with Python and programming languages in general so I'm finding difficulty in doing this. This is the code I'm using to generate the string:

from string import Template
s = Template('$who has $amount dollars.')
s.substitute(who='Merideth', amount='65')

Fairly simple, but how do I take that all and store it in a variable? I'd like to store the string in a variable and print it later.

Thanks in advance. Hate to bother with a beginner question, but we all have to start somewhere...


's.template' contains the original string passed to Template().
s.substitute() returns a string object which can be assigned to a variable.
HTH

solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 
's.template' contains the original string passed to Template(). s.substitute() returns a string object which can be assigned to a variable.

Yes, and it doesn't actually change s itself either. One confusing-but-it-makes-sense-when-you-get-used-to-it feature about Python is that strings areimmutable, which means that all string methods like substitute() return their results instead of changing the original.

Jeff

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

The Python language itself depends heavily on highspeed mappings called dictionaries. Strings are used as keys and have to be immutable objects. Jeff is right, it takes a while to sink in.

You actually should store your result in a variable ...

from string import Template
 
s = Template('$who has $amount dollars.')
str1 = s.substitute(who='Merideth', amount=65)
#same as --> str1 = s.substitute({'who': 'Merideth', 'amount': 65})
 
print str1  # Merideth has 65 dollars.
vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 

The Python language itself depends heavily on highspeed mappings called dictionaries. Strings are used as keys and have to be immutable objects. Jeff is right, it takes a while to sink in. You actually should store your result in a variable ...

from string import Template
 
s = Template('$who has $amount dollars.')
str1 = s.substitute(who='Merideth', amount=65)
#same as --> str1 = s.substitute({'who': 'Merideth', 'amount': 65})
 
print str1  # Merideth has 65 dollars.

Yes, that was exactly what I was looking for! Funny how I try everythingbut that. Thanks a bunch.

AikoYamamato
Newbie Poster
2 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You