Python here document without newlines at top and bottom

How about this?

print '''
dog
cat
'''[1:-1]

Or so long as there’s no indentation on the first line or trailing space on the last:

print '''
dog
cat
'''.strip()

Or even, if you don’t mind a bit more clutter before and after your string in exchange for being able to nicely indent it:

from textwrap import dedent

...

print dedent('''
    dog
    cat
    rabbit
    fox
''').strip()

Leave a Comment