Open In App

Python | sep parameter in print()

Last Updated : 11 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The separator between the arguments to print() function in Python is space by default (softspace feature) , which can be modified and can be made to any character, integer or string as per our choice. The ‘sep’ parameter is used to achieve the same, it is found only in python 3.x or later. It is also used for formatting the output strings.
 

Examples:

Python3




#code for disabling the softspace feature
print('G','F','G', sep='')
  
#for formatting a date
print('09','12','2016', sep='-')
  
#another example
print('pratik','geeksforgeeks', sep='@')


Output: 
 

GFG
09-12-2016
pratik@geeksforgeeks

The sep parameter when used with the end parameter it produces awesome results. Some examples by combining the sep and end parameters.
 

Python3




print('G','F', sep='', end='')
print('G')
#\n provides new line after printing the year
print('09','12','2016', sep='-', end='\n')
  
print('prtk','agarwal', sep='', end='@')
print('geeksforgeeks')


Output: 
 

GFG
09-12-2016
prtkagarwal@geeksforgeeks

Note: Please change the language from Python to Python 3 in the online ide. 
Go to your interactive python ide by typing python in your cmd ( windows ) or terminal ( linux )
 

Python3




#import the below module and see what happens
import antigravity
#NOTE - it wont work on online ide


using the sep parameter in the print() function:

By default, the sep parameter is set to a space character, so if you don’t specify it explicitly, the values will be separated by a space.

Approach:
The code is using the print() function to print out strings with different separators. The sep parameter of the print() function is used to specify the separator between the strings. In the first example, a comma is used as the separator, in the second example, a semicolon is used, and in the third example, an emoji is used.

Time Complexity:
The time complexity of the print() function is O(n), where n is the total number of characters to be printed. However, the time complexity of specifying a separator is O(1), as it is a constant time operation.

Space Complexity:
The space complexity of the code is also O(n), where n is the total number of characters to be printed. This is because the print() function needs to allocate memory to store the strings and separators before printing them out.

Overall, the code has a constant time complexity for specifying the separator, and a linear time and space complexity for printing out the strings and separators.
 

Python3




# using a comma separator
print('apples', 'oranges', 'bananas', sep=', ')
# output: apples, oranges, bananas
  
# using a semicolon separator
print('one', 'two', 'three', sep=';')
# output: one;two;three
  
# using an emoji separator
print('????', '????', '????', sep='????')
# output: ????????????????????


Output

apples, oranges, bananas
one;two;three
????????????????????


Similar Reads

Difference between end and sep in Python
In this article we will discuss the difference between The end and sep are two parameters in Python's built-in print() function that will help to control how the output is formatted. end in PythonThe end is a parameter in Python's built-in print() function that controls what character(s) are printed at the end of the printed text. The end is set to
2 min read
Python end parameter in print()
By default Python's print() function ends with a newline. A programmer with C/C++ background may wonder how to print without a newline. Python's print() function comes with a parameter called 'end'. By default, the value of this parameter is '\n', i.e. the new line character. Example 1: Here, we can end a print statement with any character/string u
2 min read
file parameter of Python's print() Function
print() function in Python3 supports a ‘file‘ argument, which specifies where the function should write a given object(s) to. If not specified explicitly, it is sys.stdout by default. It serves two essential purposes: Print to STDERRPrint to external fileNote: The 'file' parameter is found only in Python 3.x or later. Printing to STDERRSpecify the
5 min read
Python | Key-Value to URL Parameter Conversion
Many times, while working in the web development domain, we can encounter a problem in which we require to set as URL parameters some of the key-value pairs we have, either in form of tuples, or a key and value list. Let's discuss a solution for both cases. Method #1: Using urllib.urlencode() ( with tuples ) The urlencode function is root function
5 min read
Python | Split and Pass list as separate parameter
With the advent of programming paradigms, there has been need to modify the way one codes. One such paradigm is OOPS. In this, we have a technique called modularity, which stands for making different modules/functions which perform independent tasks in program. In this, we need to pass more than just variable, but a list as well. Let's discuss cert
4 min read
Transfer Kwargs Parameter to a Different Function in Python
In Python, **kwargs is a special syntax that allows a function to accept any number of keyword arguments. When transferring **kwargs parameters to a different function, involves passing the received keyword arguments to another function using the same **kwargs syntax. In this article, we will explore different approaches to pass kwargs as a Paramet
3 min read
Use of na_values parameter in read_csv() function of Pandas in Python
read_csv() is an important pandas function to read CSV files. But there are many other things one can do through this function only to change the returned object completely. In this post, we will see the use of the na_values parameter. na_values: This is used to create a string that considers pandas as NaN (Not a Number). by-default pandas consider
1 min read
Positional-only Parameter in Python3.8
Python introduces the new function syntax in Python3.8.2 Version, Where we can introduce the / forward slash to compare the positional only parameter which comes before the / slash and parameters that comes after * is keyword only arguments. Rest of the arguments that are come between / and * can be either positional or keyword type of argument. Th
2 min read
Mahotas - Parameter-Free Threshold Adjacency Statistics
In this article we will see how we can get the image's parameter-free threshold adjacency statistics in mahotas. TAS were presented by Hamilton et al. in “Fast automated cell phenotype image classification” For this tutorial we will use 'lena' image, below is the command to load the lena image mahotas.demos.load('lena') Below is the lena image In o
2 min read
Python Program to print digit pattern
The program must accept an integer N as the input. The program must print the desired pattern as shown in the example input/ output. Examples: Input : 41325 Output : |**** |* |*** |** |***** Explanation: for a given integer print the number of *'s that are equivalent to each digit in the integer. Here the first digit is 4 so print four *sin the fir
3 min read
Article Tags :
Practice Tags :