Storing a Template String?

Thread Solved

Join Date: Mar 2007
Posts: 2
Reputation: AikoYamamato is an unknown quantity at this point 
Solved Threads: 0
AikoYamamato AikoYamamato is offline Offline
Newbie Poster

Storing a Template String?

 
0
  #1
Mar 13th, 2007
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:

  1. from string import Template
  2. s = Template('$who has $amount dollars.')
  3. 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...
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Storing a Template String?

 
0
  #2
Mar 13th, 2007
Originally Posted by AikoYamamato View Post
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:

  1. from string import Template
  2. s = Template('$who has $amount dollars.')
  3. 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Storing a Template String?

 
0
  #3
Mar 13th, 2007
'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 are immutable, which means that all string methods like substitute() return their results instead of changing the original.

Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Storing a Template String?

 
0
  #4
Mar 13th, 2007
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 ...
  1. from string import Template
  2.  
  3. s = Template('$who has $amount dollars.')
  4. str1 = s.substitute(who='Merideth', amount=65)
  5. #same as --> str1 = s.substitute({'who': 'Merideth', 'amount': 65})
  6.  
  7. print str1 # Merideth has 65 dollars.
Last edited by vegaseat; Mar 13th, 2007 at 9:46 pm. Reason: code
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: AikoYamamato is an unknown quantity at this point 
Solved Threads: 0
AikoYamamato AikoYamamato is offline Offline
Newbie Poster

Re: Storing a Template String?

 
0
  #5
Mar 15th, 2007
Originally Posted by vegaseat View Post
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 ...
  1. from string import Template
  2.  
  3. s = Template('$who has $amount dollars.')
  4. str1 = s.substitute(who='Merideth', amount=65)
  5. #same as --> str1 = s.substitute({'who': 'Merideth', 'amount': 65})
  6.  
  7. print str1 # Merideth has 65 dollars.
Yes, that was exactly what I was looking for! Funny how I try everything but that. Thanks a bunch.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC