Select Page

Multiple Lines or Paragraph Printing in Python

Doesn’t it feel weird to use the print function again and again when displaying multiple lines at once or a paragraph? It reduces the readability of your script. In this tutorial, you’ll learn about all the possible ways of printing multiple lines or a paragraph using only one print function.

Newline Character to Print Multiple Lines

When writing a text file or document, you always press the Enter or Return button to include one new line and the cursor goes to that new line. But how does your computer or phone knows when reading a file that, this text should be on line 1, this text on line 2, and so on? Although you don’t see anything, when you press the Enter or Return button, your editor includes a newline character to the file. This newline character is presented by \n (backslash+n).

Although you don’t see newline characters being added in the text editors, this can be seen in the command line. If you remember from the cat command tutorial, it can show the newline character as $. Here’s an example:

Showing end of line in command line
Showing End of Line

Now, use one print function to display a paragraph, just add newline character (\n) before each line. If you add 2 newline characters at once, one will show an empty line. Let’s see.

print('Line1\nLine2\n\nLine3\nLine4')

Notice the output:

printing multiple lines at once in Python with newline characters
Multiple lines with newline character

SEP Argument in Print Function

When you pass a string to a print function, python displays that string as output. What if you pass multiple strings at once, and separate them by commas?

print('Line 1', 'Line 2', 'Line 3')

What do you see? They are getting added to each other and separated by whitespace. This is a very useful feature of the print function. Because you even can add any type of data in this way. But let’s get back to our main discussion.

Inside the print function, you can pass values to some arguments. One is the sep argument, which stands for separator. Whatever you pass here, all the strings will be separated by this value. Let’s use sep=’#’ in the above code:

print('Line 1', 'Line 2', 'Line 3', sep='#')

Remember, the separator must be a string. Otherwise you’ll get an error.

What if you pass a newline character to it? Shouldn’t all the strings get printed in different lines? Let’s try it:

print('Line 1', 'Line 2', 'Line 3', sep='\n')

Here’s all the outputs at once:

python print function with sep argument for printing a paragraph at once
print function with sep argument

Don’t forget to use the sep argument with different strings!

Triple Quotes for Paragraph Printing

If you wrap a paragraph with triple quotes, it becomes a paragraph string. Then this can be easily printed. You can use either single or double quotes thrice for this purpose. Try this out:

print('''Line 1
Line 2
Line 3
''')

You can start line one from the next line. But notice the output:

print("""
Line 1
Line 2
Line 3
""")

You’ll see a blank line before the paragraph. To avoid this, add a backslash (\) in the first line.

print('''\
Line 1
Line 2
Line 3
''')

Here’s all the outputs in the online text editor.

Python Paragraph printing with triple quotes
Paragraph printing with triple quotes

Python join Method

The Python join method has many use cases, which will be discussed later. But you can use it for printing multiple lines at once. The basic structure of Python join method is as follows:

separator.join(iterable)

Iterable includes any data type that contains multiple values and all the values can be extracted with a for loop. For example, String, List, Tuple, Set, Dictionary.

How can you use the join method to print multiple lines at once? Take all the lines in any iterable, like list, and use the newline character (\n) as a separator. The join method joins all the values of iterable and separate them with the separator. Here’s how:

print('\n'.join(['Line 1', 'Line 2', 'Line 3']))

Alternatively, you can use variable to make it clearer.

iterable = ['Line 1', 'Line 2', 'Line 3']
separator = '\n'
print(separator.join(iterable))

And the output:

multiline printing with join method
join method for multi-line printing

Although it seems similar to the sep argument of print function, the join method has a limitation. You can only join strings with this. If any of the elements include a non string value, Python will raise an error.

iterable = ['Line 1', 'Line 2', 'Line 3', 4]
separator = '\n'
print(iterable.join(separator))

And the output:

join method limitation
join method limitation

Notice that the error explains what’s the problem here. It’s a type error. List is a sequence of values, and the problem is on the 3rd element (counting starts from 0). join expected a string value, but found an integer, i.e. a number.

Extra but Important Information

Formatting a string for printing

When you wrap a text in quotation marks, i.e. create a string, you may need to add some modification within it. I mean, say you want to add a variable, or repeat a character 100 times, how would you do it? Just use curly brackets and write your code within it. Python will execute this part and replace the entire curly braces. Here’s what I mean.

Say you want to print this message:

--------------------
# User: Fahim
# ID: 42
# Language: Python

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

Here the User, ID, Language can differ for different users. And then, there are 20 dashes above and below the text, will you write them one by one? Let’s see a good solution, with just one print function. (Variables and string formatting coming soon)

user, id, lan = 'Fahim', 42, 'Python'   # multiple assignment
info = """\
{'-'*40}
# User: {user}
# ID: {id}
# Language: {lan}

{'-'*40}
"""

print(info)

Paragraph Comment vs Paragraph Printing

You might get confused about the usage of triple quotes, aren’t you? Triple quotes include Triple Single Quotes (”’ ”’) and Triple Double Quotes (“”” “””). They are used both in printing paragraph and making paragraph comment. Look closely, both of them are strings. And when this string is used in a variable, function, or anything else, it behaves as a value. But when you leave it alone, it behaves as a comment. Remember from Comments in Python tutorial, that unassigned string behaves as comment? This is the same thing! So, don’t get confused again!

paragraph comment vs paragraph printing in python
multiline comment vs multiline printing

If your string includes single or double quote for some reason, use triple quotes to wrap it even if it’s just a one line string. In this way, you don’t need to worry about using exclusive quotes in your string.

print('''I'm a "Python" programmer.''')

Conclusion

Practice all of the given examples and create some new examples. If you’re new to Python programming, try reading these tutorials:

Mind Sharing It?
Join Facebook Group

Public Group

A group optimized for learning purposes. Track your progress by joining a unit

Follow on Quora

Public Space

Best place to ask questions! Join the built-in community to get help from others