Question

In PHP I can do this:

print <<<END
This uses the "here document" syntax to output
multiple lines
END;

Is there a way to do something similar in Python?

I want to dump a big chunk of HTML without worrying about escaping e.g. angle brackets.

Was it helpful?

Solution

Python doesn't have heredocs with arbitrary start/end markers. You can use triple quotes (either single or double) to achieve something very similar:

print '''
This uses the "here document" syntax to output
multiple lines
'''

Add an r to make it a raw string so that backslash is also not treated specially. r'''....'''

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top