70

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 in Python 3.5.1.

3
  • 1
    Yes there is and it is called a for loop... Commented Jan 24, 2016 at 19:19
  • 2
    This can be solved with a simple Google search. "\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. Commented Jan 24, 2016 at 19:20
  • You should have included how you are printing multiple lines with a print for every line. The proper answer depends on where you are starting from. Commented Jan 25, 2016 at 7:57

7 Answers 7

110

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

a = """
text
text
text
"""

print(a)
Sign up to request clarification or add additional context in comments.

2 Comments

this also works with f strings and raised errors
This can be paired with textwrap.dedent(a) to remove the indentation. Or even better - use print's argument sep=os.linesep as in Quba's answer.
49

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

3 Comments

This (especially the first two methods) is very useful for packages so that indentation does not get messed up.
Why use os.linesep instead of "\n"? AFAIK that works on all platforms including Windows
@qwr This answer is 9 years old, so we’re a bit late to the party, but the docs for the os module in Python 3.13 explicitly state "Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms." (and that phrase has been there since at least 2007!)
16

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)

1 Comment

Any idea how to put an end to the back of these? I would like to do sometimes like this while True: time.sleep(1) direction = ["left", "right", "up", "down"] x = random.choice(direction) print(x, end="\r") thanks!
7

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

1 Comment

This might require adding a call to str() on the elements of the iterable, since print would normally do that, but join doesn't. print("\n".join([str(x) for x in iterable])
0

Adding \n in the string adds a new line.

For example,

print("Adds one extra line after the text\n")
print("Adds two extra lines after the text\n\n")
print("Adds three extra lines after the text\n\n\n")
print("And so on...")

Comments

0

Somehow indent preserving way is not yet mentioned:

print('This is a long string that is split \n'
      'across multiple lines using parentheses')

This is a long string that is split
across multiple lines using parentheses

this works because consequent string literals are implicitly joined:

msg = 'Space after newline symbol \n' 'improves readability'
print(msg) 

Space after newline symbol
improves readability

1 Comment

Indeed, this option was missing - but I believe you could explain it better (what the language does when finding consecutive strings, why are the parentheses needed, etc...) - it will deserve some extra upvotes if so.
-1

For ASCII art you do not want escape char be and tried resolved, so putting "r" before the triple quotes tells Python it is a "raw" format multi-line string, like:

print(r""" your art here """)

Comments

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.