Mastering Python’s print() Function: Using the sep Parameter with Examples

The separator between the arguments of the print() function in Python is a space by default (a software space feature), it can be modified, and it can be set to any character, integer or string according to our choice. The “sep” parameter is used to implement the same functionality and is only found in python 3.x or later. It is also used to format the output string.

Example:

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

The output is as follows:

GFG
09-12-2016
pratik@lsbin

When the sep parameter is used with the end parameter, it produces amazing results. Some examples of combining sep and end parameters.

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

The output is as follows:

GFG
09-12-2016
prtkagarwal@lsbin

Note: Please change the language from Python to Python 3 in the online ide.

Type Python in cmd (Windows) or terminal (linux) to go to the interactive Python IDE.

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

If you find anything incorrect, or would like to share more information about the above topics, please write a comment.

First, your interview preparation enhances your data structure concepts with the Python DS course.