multi-quoted strings for HERE documents

Bengt Richter bokr at oz.net
Mon Nov 18 21:03:45 EST 2002


On 19 Nov 2002 00:40:45 GMT, "Michael P. Soulier" <msoulier at storm.ca._nospam> wrote:

>    Hey people,
>
>    At work I do a mix of Perl and Python. I'm a big fan of the latter.
>However, there's one thing that I like in Perl that I haven't quite gotten the
>same feel out of Python yet. That would be HERE documents. 
>
>ie. 
>
>print<<EOF;
><table>
>    <tr>
>        <td>cell contents</td>
>    </r>
></table>
>EOF
>
>    Now, I can get close in Python with multi-line strings, like so...
>
>print """
><table>
>    <tr>
>        <td>cell contents</td>
>    </r>
></table>"""
>
>    But, there's an initial newline in the output because of where the
>beginning quotes are. I can put the quotes on the next line to avoid that...
>
>print \
>"""<table>
>etc

so close ;-)

 >>> print """\
 ... <table>
 ...     <tr>
 ...         <td>cell contents</td>
 ...     </r>
 ... </table>"""
 <table>
     <tr>
         <td>cell contents</td>
     </r>
 </table>


 >>> """\
 ... <table>
 ...     <tr>
 ...         <td>cell contents</td>
 ...     </r>
 ... </table>"""
 '<table>\n    <tr>\n        <td>cell contents</td>\n    </r>\n</table>'

BTW, are you sure you didn't want a last \n before the final """ ?

I've wanted HERE document delimiting too, but mainly for the ability to
choose an arbitrary delimiter.

Regards,
Bengt Richter



More information about the Python-list mailing list