| | |
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: 149
•
•
•
•
'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 |
abrupt accessdenied ansi anti apache application approximation argv array assignment backend beginner binary bluetooth builtin calculator change character converter countpasswordentry curved customdialog dan08 dictionary dynamic edit exe file float format function gnu graphics heads homework inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook pointer prime programming progressbar py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session software sprite statictext statistics string strings syntax terminal text thread threading time tlapse tuple twoup ubuntu unicode unit urllib urllib2 variable wordgame write wxpython xlib






