| | |
Storing a Template String?
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
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:
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...
Python Syntax (Toggle Plain Text)
from string import Template s = Template('$who has $amount dollars.') s.substitute(who='Merideth', amount='65')
Thanks in advance. Hate to bother with a beginner question, but we all have to start somewhere...
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
•
•
•
•
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:
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.Python Syntax (Toggle Plain Text)
from string import Template s = Template('$who has $amount dollars.') s.substitute(who='Merideth', amount='65')
Thanks in advance. Hate to bother with a beginner question, but we all have to start somewhere...
s.substitute() returns a string object which can be assigned to a variable.
HTH
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
•
•
•
•
's.template' contains the original string passed to Template().
s.substitute() returns a string object which can be assigned to a variable.
Jeff
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 ...
You actually should store your result in a variable ...
python Syntax (Toggle Plain Text)
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.
Last edited by vegaseat; Mar 13th, 2007 at 9:46 pm. Reason: code
May 'the Google' be with you!
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
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)
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.
![]() |
Similar Threads
- Array/String intersect and Array/Array intersect code (C#)
- How to Hash and Store in Database (ASP.NET)
- Is this due to being out of memory? (C)
- Help on error message, illegal start an ';' expected (Java)
- accessing private data members (C++)
Other Threads in the Python Forum
- Previous Thread: entry widget
- Next Thread: python c interface problems
| Thread Tools | Search this Thread |
accessdenied advanced apache application argv beginner change command convert csv cursor def dictionary digital dynamic dynamically edit enter event examples file float format frange function google gui homework i/o import inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame newb number numbers numeric obexftp output parameters parsing path phonebook port prime programming projects py2exe pygame pygtk pyopengl python random recursion redirect remote return reverse scrolledtext session simple skinning software sprite statictext string strings syntax terminal text threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape wordgame wxpython






