How to print multiple lines of text using python

If I wanted to print multiple lines of text in Python without typing print('') for each line, is there a way to do this? I use this for ASCII art.

(python 3.5.1)

+12
python
source share
3 answers

You can use triple quotes (single or double):

 a = """ text text text """ print(a) 
+28
source share

As far as I know, there are 3 different ways.

Use \n in your print

 'print("first line\nSecond line")' 

use sep="\n" in print

 'print("first line", "second line", sep="\n")' 

Use triple quotes and multiline strings

 print(""" Line1 line2 """") 
+5
source share

The answer to triple quotes is great for ascii art, but for those who wonder: what if my multiple lines are a tuple, list, or other iterable that returns the lines (maybe understanding the list?), And then:

 print("\n".join(<*iterable*>)) 

For example:

 print("\n".join([ "{}={}".format(k, v) for k, v in os.environ.items() if 'PATH' in k ])) 
+1
source share

All Articles