Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python here document without newlines at top and bottom

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.

like image 267
Juan Avatar asked Mar 06 '12 18:03

Juan


1 Answers

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() 
like image 62
Weeble Avatar answered Sep 22 '22 07:09

Weeble