‘sep’ and ‘end’ parameter in print() function Python
In this tutorial, we are going to learn about the print function parameters i.e sep and end. Earlier we don’t actually have the print as a function but as a statement. So it becomes difficult to format and print the text and echo on the screen.
But now in Python 3.x print comes out to be a function that has the following parameters that needs to pass to it while calling it as it is a function. Print function acts as a standard output device in Python Version 3.x
Parameters inside the Print function i.e. Sep and End
print ( <variable name or string> , sep = <separator value> , end = <end value >)
Sep Parameter in Python
As the name suggests, sep is used for adding separators into the string provided. Basically, it acts as a simple tool to modify the strings by replacing all spaces inside strings with the specified separator as an input value to the “sep” parameter. If we don’t specify any value to the “sep” parameter the by default it is considered to be a no-space.
#code for using the default parameter print('C','O','D','E', sep='') #for formatting today's date using backslash print('19','02','2019', sep='/') #formatting using @ symbol print('pavitra','codespeedy', sep='@')
CODE 19/02/2019 [email protected]
End Parameter in Python
End, as the name suggests, is used for ending pattern of the string supplied during input . Basically, it acts as a simple tool to modify the strings by formatting the strings in a specified format by the help of specified parameter as an input value to the “end” parameter. If we don’t specify any value to the “end” parameter the by default it is considered to be a new line character i.e. “\n”.
Earlier in python 2.x we used to supply a comma to the print statement to keep the output in the same line. But here we will be supplying empty strings to end so that the text is echoed in the same line.
# ends the output with a <white space> print("Welcome to" , end = ' ') print("Code Speedy") # no value of end is assigned and hence newline by default # ends the output with '#' hash print("Pavitra" , end = '#') print("Code Speedy")
Welcome to Code Speedy Pavitra#Code Speedy