| | |
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 |
address alarm anydbm app beginner changecolor cipher clear conversion coordinates corners curves cx-freeze data definedlines development dictionary directory dynamic events excel feet file float format function generator getvalue halp handling homework images import input ip itunes java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse number numbers output parsing path port prime programming py2exe pygame pymailer python queue random rational raw_input recursion recursive screensaverloopinactive script scrolledtext searchingfile slicenotation split ssh string strings tails text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables ventrilo vigenere web webservice word wxpython xlwt






