Coming from Perl

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Sep 12 21:11:56 EDT 2007


Amer Neely <perl4hire at softouch.on.ca> writes:

> print <<EndHTML;
> <html>
> <body>
> Hello
> </body>
> </html>
> EndHTML

The equivalent in Python would be::

    print """
    <html>
    <body>
    Hello
    </body>
    </html>
    """

You can even use Python's standard textwrap module to indent your code
properly::

    import textwrap

    def foo(bar):
        if baz:
            print textwrap.dedent("""
                <html>
                <body>
                    Hello
                </body>
                </html>
                """)

The 'textwrap.dedent' function strips *common* leading whitespace from
all lines in the string, so in the above example the resulting string
will be as though the original string were typed flush to the left
margin.

See 'help(textwrap.dedent)' for what that function does, or read it
online <URL:http://docs.python.org/lib/module-textwrap>.

-- 
 \            "Madness is rare in individuals, but in groups, parties, |
  `\         nations and ages it is the rule."  -- Friedrich Nietzsche |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list