sep Parameter in print() in Python

Introduction:

In this tutorial, we are learning about the sep parameter in print() in Python. The separator of the argument of the print() function in Python is set by default with a space (soft space feature), which can be changed and set to any character, number or string as per our preference. The sep parameter is used to accomplish the same purpose and appears only in Python 3.x or later. It is also used to format output strings. The sep parameter allows you to specify a string, which will be used to separate the elements printed by the print() function. The sep parameter is set to a single space character by default. This means that printed items will leave the site. But you can adjust the sep parameter.

Syntax:

The syntax of the sep parameter in print() in Python is given below -

Program Code:

Here, we give a program code of the sep parameter in print() in Python. The code is given below -

Output:

Now we run the above code and find the result from it. The result is given below -

JTP
05-03-2024
Priyanka;Javatpoint

What are the differences between the sep parameter and end parameter in print() in Python?

The end and sep parameters in the Python print function are different due to their locations. They represent the places where they are used in the work. The end parameter specifies the string to be printed after output. However, the sep parameter separates the items to be printed.

The sep parameter is used to specify a string as a separator between printed items. The default value is "". On the other side, the end parameter is used to specify the string as a terminator after printing the object. The default value of the end parameter is "\n". The sep and end parameters in the print() function specify the separator between arguments. The separator is a space character by default.

Program Code:

Here, we give a program code to show the use of the sep and end parameters in the Python print function. The code is given below -

Output:

Now we run the above code and find the result from it. The result is given below -

JTP
05-03-2024
Priyanka05@javatpoint
Welcome to javatpoint-Hello Coders@>

Now, we have changed the language from Python to Python 3 in the online IDE. To access the interactive Python IDE, type Python in cmd (Windows) or terminal (Linux). Here, we import a module and see what changes have happened. This module does not work in the Python online IDE. The module is given below -

The sep parameter used in the print() function:

By default, the sep parameter in Python is set to a space character. So, if you do not specify one, the results will be separated by spaces. This code uses the print() function in Python to print the strings with different separators. In the print() function, the sep parameter is used to specify the separator between strings. In the first example, a semicolon is used as a separator; in the second example, a comma is used as a separator; and in the third example, an emoji is used.

Time and space complexity:

The time complexity of the print() function in Python is represented by O(n). Here, n is the total number of characters to print. However, the time complexity of the code is O(1) since there is a fixed processing time. On the other side, the space complexity of the print() function in Python is also represented by O(n). Here, n is the total number of characters. This is because the print() function in Python needs to allocate the memory to store strings and separate them before printing. Generally, the code has constant time complexity for limit and linear time and space complexity for printing strings and separators.

Program Code:

Here, we give a program code of the sep parameter in the Python print() function. The code is given below -

Output:

Now we run the above code and find the result from it. The result is given below -

Rose; Lilly; Potato
A, C, E
####~~~~****~~~~####