Python sep (sep=) Parameter in print()

The sep parameter in print() is used to separate multiple parameters using the value assigned to it. For example, the following Python code:

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

produces following output:

python sep parameter

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

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

Now the output produced by this program will be:

sep parameter python

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

Use of sep Parameter

The use of sep parameter is not limited to only remove 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 like 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 Escape Sequence using sep Parameter

You can also use escape sequence characters using sep parameter in print(). To illustrate it, first let's take an example without assigning 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 (backslash character) assigned to sep, created after modifying the above program:

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

This time, the program produces 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 sometime when you need this type of outputs.

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

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

This program produces following output:


Python Online Test

« Previous Tutorial Next Tutorial »

© Copyright 2021. All Rights Reserved.