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
print ( 'G' , 'F' , 'G' , sep = '')
print ( '09' , '12' , '2016' , sep = '-' )
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' )
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 )
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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
print ( 'apples' , 'oranges' , 'bananas' , sep = ', ' )
print ( 'one' , 'two' , 'three' , sep = ';' )
print ( '????' , '????' , '????' , sep = '????' )
|
Output
apples, oranges, bananas
one;two;three
????????????????????
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!