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)
"\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.
To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")
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.
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.
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 """)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With