Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print multiple lines of text with Python

If I wanted to print multiple lines of text in Python without typing print('') for every line, is there a way to do that?

I'm using this for ASCII art.

(Python 3.5.1)

like image 840
Boio Avatar asked Jan 24 '16 19:01

Boio


People also ask

How do you print multiple lines in Python?

"\n" can be used for a new line character, or if you are printing the same thing multiple times then a for loop should be used.

How do I print multiple lines on one line in Python?

To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")

How do you print text lines in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.

How do you write multiple lines on a string in Python?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.


2 Answers

You can use triple quotes (single ' or double "):

a = """ text text text """  print(a) 
like image 68
JRazor Avatar answered Sep 25 '22 15:09

JRazor


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

Use os.linesep in your print:

print(f"first line{os.linesep}Second line") 

Use sep=os.linesep in print:

print("first line", "second line", sep=os.linesep) 

Use triple quotes and a multiline string:

print(""" Line1 Line2 """) 
like image 28
Quba Avatar answered Sep 23 '22 15:09

Quba