61

What's the best way to have a here document, without newlines at the top and bottom? For example:

print '''
dog
cat
'''

will have newlines at the top and bottom, and to get rid of them I have to do this:

print '''dog
cat'''

which I find to be much less readable.

1
  • 2
    print '''\ dog cat\ ''' just like doing line continuation Commented Apr 18, 2013 at 10:00

6 Answers 6

84

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()
Sign up to request clarification or add additional context in comments.

1 Comment

I think your first approach is the most elegant, since you only affect the first and last whitespace character. I was using strip before through a function call.
41

Add backslash \ at the end of unwanted lines:

 text = '''\
 cat
 dog\
 '''

It is somewhat more readable.

5 Comments

The backslash at the end of line dog makes it go away. Since this is not a raw string, \[newline] is reduced to nothing
It ends up looking awfully funny when the variable assignment is indented deeper than the module level (eg a method of a class).
Thank you, exactly what I was looking for. I use here docs in Ruby and Bash (which do not add a newline before the first line), and it seems inelegant to remove it with a [1:] or strip().
Yep, but this is not exactly a pure here document. Nice idea anyway, some might like it.
Has the behaviour changed with Python 3? At least for raw-strings it doesn't seem to work any longer.
23

use parentheses:

print (
'''dog
cat'''
)

Use str.strip()

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

use str.join()

print '\n'.join((
    'dog',
    'cat',
    ))

1 Comment

Parens I think is the way to go. It's readable, it's more concise than the strip method, more readable than the \n for long blocks, and it seems to conform to the official coding standards.
7

You could use strip():

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

Comments

1

Use a backslash at the start of the first line to avoid the first newline, and use the "end" modifier at the end to avoid the last:

    print ('''\
    dog
    cat
    ''', end='')

Comments

-4

Do you actually need the multi-line syntax? Why not just emded a newline?

I find print "dog\ncat" far more readable than either.

3 Comments

Mileage varies. He's asking heredoc, so let's give him that :)
pretty sure he wants to print out more than cat dog. I know that's why I'm here
They asked for help, not judgment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.