943,704 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1823
  • Python RSS
Mar 13th, 2007
0

Storing a Template String?

Expand 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:

Python Syntax (Toggle Plain Text)
  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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AikoYamamato is offline Offline
2 posts
since Mar 2007
Mar 13th, 2007
0

Re: 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:

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Mar 13th, 2007
0

Re: Storing a Template String?

Quote ...
'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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 13th, 2007
0

Re: Storing a Template String?

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 ...
python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 15th, 2007
0

Re: Storing a Template String?

Click to Expand / Collapse  Quote originally posted by vegaseat ...
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 ...
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AikoYamamato is offline Offline
2 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: entry widget
Next Thread in Python Forum Timeline: python c interface problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC