Easy "here documents" ??

Jim Sizelove sizelji at insightbb.com
Tue Dec 21 08:34:14 EST 2004


Doug Holton wrote:
> Bengt Richter wrote:
> 
>>> variable1 = 1
>>> variable2 = 2
>>>
>>> s = """
>>>   v = ${variable1}
>>>   v2's value is: ${variable2}
>>> """
>>>
>>> However, Python 3.0 is likely years away.  If you want to know how to 
>>> run code like this today, consult Fredrik Lundh.
>>
>>
>>
>> Or replace ${...} with equally simple %(...)s in the above and be 
>> happy ;-)
> 
> 
> I'm afraid you are incorrect.  Simply replacing the $ with % in my 
> example will not work in Python.
> If you would like to use % instead of $, I recommend requesting that 
> feature for Python 3.0: http://www.python.org/cgi-bin/moinmoin/Python3.0

Oh, but it does work:

     >>> variable1 = 1
     >>> variable2 = 2
     >>> s = """
     ...    v1 = %(variable1)s
     ...    v2's value is: %(variable2)s
     ... """
     >>> print s % vars()

        v1 = 1
        v2's value is: 2

--Jim



More information about the Python-list mailing list