How to print multiple lines of text with Python
Question:
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)
Answers:
You can use triple quotes (single ‘ or double "):
a = """
text
text
text
"""
print(a)
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
""")
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]))
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)
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 """)
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)
You can use triple quotes (single ‘ or double "):
a = """
text
text
text
"""
print(a)
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
""")
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]))
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)
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 """)