heredoc and variables

Xavier Martínez xavim at fqingenieria.es
Mon Jun 7 07:53:10 EDT 2004


Michael Geary wrote:

> Now that I think of it, if you do want to get rid of both newlines, 
> here are
> a couple of other ways to do it:
>
> end_html = """\
> </BODY>
> </HTML>\
> """
>
> That is a bit ugly, but this seems reasonable (and better than the [1:-1]
> slice trick):
>
> end_html = """\
> </BODY>
> </HTML>"""
>
> -Mike
>
>
Just for completeness, you can also use lstrip(), rstrip() and strip() 
string methods to remove whitespace at the beggining, end or both parts 
of a string, respectively.  I.e.:

end_html = """
</BODY>
</HTML>
""".lstrip()

will get rid of the beginning newline.  Using strip(), will get rid of 
both.

I still prefer Mike's backslash solution, as getting rid of the newline 
is done at parse time rather than at run time.  Also lstrip() will eat 
too much space if the first line of the string ("</BODY") is indented 
(and you want to preserve that indentation).

- Xavier

-------------- next part --------------
A non-text attachment was scrubbed...
Name: xavim.vcf
Type: text/x-vcard
Size: 268 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040607/a3416df1/attachment.vcf>


More information about the Python-list mailing list