M&C11: Print multiple-line strings

The string format operator % is used in Python to format output. There exist many good tutorials for printing in Python for the interested student. We mention one printing feature that can be convenient: triple quotes.

Sometimes we want to print several lines of text or a paragraph in a program. We can do so using several print statements:

print 'The book title is Python Programming for Beginners.'

print 'The author is Bill Gates.'

print 'It is published by Microsoft.'

Python provides a way to create strings to span multiple lines. You can use triple quotes to create multiple-line strings as shown below:

paragraph = '''The book title is Python Programming for Beginners.
The author is Bill Gates.
It is published by Microsoft.
'''

print paragraph

Another way to print a multiple-line string is using backslash notation. Adding \n into your string create newlines. See the code below:

paragraph = 'The book title is Python Programming for Beginners.\nThe author is Bill Gates.\nIt is published by Microsoft.'

print paragraph

The three ways above produce the same result:

The book title is Python Programming for Beginners.
The author is Bill Gates.
It is published by Microsoft.