Can you print multiple lines in 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)

Can you print multiple lines in python?

asked Jan 24, 2016 at 19:17

3

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

a = """
text
text
text
"""

print(a)

Can you print multiple lines in python?

answered Jan 24, 2016 at 19:20

JRazorJRazor

2,57817 silver badges26 bronze badges

1

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
""")

Gulzar

19k19 gold badges95 silver badges160 bronze badges

answered Jan 24, 2016 at 19:20

Can you print multiple lines in python?

QubaQuba

4,0966 gold badges34 silver badges55 bronze badges

1

I wanted to answer to the following question which is a little bit different than this:

Best way to print messages on multiple lines

He wanted to show lines from repeated characters too. He wanted this output:

----------------------------------------
# Operator Micro-benchmarks
# Run_mode: short
# Num_repeats: 5
# Num_runs: 1000

----------------------------------------

You can create those lines inside f-strings with a multiplication, like this:

run_mode, num_repeats, num_runs = 'short', 5, 1000

s = f"""
{'-'*40}
# Operator Micro-benchmarks
# Run_mode: {run_mode}
# Num_repeats: {num_repeats}
# Num_runs: {num_runs}

{'-'*40}
"""

print(s)

Can you print multiple lines in python?

answered Mar 14, 2019 at 16:50

1

The triple quotes answer is great for ASCII art, but for those wondering - what if my multiple lines are a tuple, list, or other iterable that returns strings (perhaps a list comprehension?), then how about:

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

For example:

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

Can you print multiple lines in python?

answered Dec 13, 2017 at 14:57

pourhauspourhaus

5176 silver badges9 bronze badges

1

I realize it is an old thread, but my comment might help someone, so here it is: for ASCII art you do not want escape char be and tried resolved, so putting "r" before the tripple quotes tells python it is a "raw" format multi-line comment, like: print(r""" your art here """)

answered Oct 18, 2021 at 16:19

Can you print multiple lines in python?

Educative Answers Team

Implementation

Code 1

To print a single line, we cover our string with single/double quotes and pass it to the print() function:

print(" Hello World ")

#or

print(' Hello Again ')

Code 2

Similarly, using triple quotes enables us to print multiple lines in one statement:

print('''

|==[[[[[]]]]]==||==[[[[[]]]]]==|

|==[[[[[]]]]]==||==[[[[[]]]]]==|

|==[[[[[]]]]]==||==[[[[[]]]]]==|

|==[[[[[]]]]]==||==[[[[[]]]]]==|

|==[[[[[]]]]]==||==[[[[[]]]]]==|

|==[[[[[]]]]]==||==[[[[[]]]]]==|

''')

Note: the triple quote must be made by three single quotes rather than a single quote and a double quote.

RELATED TAGS

print

python

multiple lines

Copyright ©2022 Educative, Inc. All rights reserved

How do you 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 5 lines in Python?

Use file..
a_file = open("file_name.txt") Open "file_name.txt".
number_of_lines = 3..
for i in range(number_of_lines): Print the first number_of_lines lines of a_file..
line = a_file. readline().
print(line).

How do you print all lines in Python?

Use file..
a_file = open("sample.txt").
lines = a_file. readlines().
for line in lines:.
print(line).
a_file. close().