Python sep (sep=) parameter in print() with examples

Using the value given to the "sep" parameter in print(), you can separate more than one parameter. For example, the following Python code

print("s", "e", "p")

produces the following output:

python sep parameter

As you can see from the above output, there is a single space inserted automatically between each parameter (that is, "s,"  "e,"  and "p"). But using the "sep" parameter, we can remove the default behavior of print() and insert an automatic space between each of its parameters. Here is the modified version of the previous program:

print("s", "e", "p", sep="")

Now, the output of this program will be:

sep parameter python

Important: The "sep=" parameter must be placed as the last parameter of print().

Use of the sep parameter

The use of the sep parameter is not limited to only removing automatically inserted spaces between all parameters of print(). But it can also be used to insert any required character or combination of characters between all parameters, as shown in the program given below:

print("My Computer", "C Drive", "Program Files", sep=" -> ")

This program produces the output as shown in the snapshot given below:

python sep parameter example

Use of the escape sequence using the sep parameter

You can also use escape sequence characters using the sep parameter in print(). To illustrate it, first let's take an example without assigning an escape sequence to sep. Here is the program:

print("s ", "e ", "p", sep="")

The above program produces:

use of sep parameter in python

Now let's create the same program with an escape sequence character, say "\b" (the backslash character), assigned to sep, created after modifying the above program:

print("s ", "e ", "p", sep="\b")

This time, the program produces the following output:

sep parameter python

Note: These are some demo programs showing you the use of sep. Or how this parameter plays an important role sometimes when you need these types of outputs.

You can also format the date or anything you need using the sep parameter in the required way, like shown in the demo program given below:

print("01", "09", "2021", sep="-")

This program produces the following output:

01-09-2021

Advantages of the sep in Python

Disadvantages of the sep in Python

Python Online Test


« Previous Tutorial Next Tutorial »