heredoc and variables

Michael Geary Mike at DeleteThis.Geary.com
Fri Jun 4 11:41:42 EDT 2004


flupke wrote:
> 2) Consider following heredoc
>
> end_html="""</BODY>
> </HTML>"""
>
> I have to write it like this to avoid having an empty line added
> above AND below the actual string when using it.
> So this would produce the extra empty lines
>
> end_html="""
> </BODY>
> </HTML>
> """
>
> I actually think that the last piece of code is more readable. Is
> there a way to stop that behaviour or is there a kind of trim
> function i can apply to get rid of the 2 extra lines?

If you can live with the newline at the end of the text (which in most cases
is what you want anyway), this is the cleanest way to do it:

end_html = """\
</BODY>
</HTML>
"""

Or, you can get rid of both newlines this way:

end_html = """
</BODY>
</HTML>
"""[1:-1]

-Mike





More information about the Python-list mailing list