End Parameter in python
print():
The python print() function prints the program’s output to the output screen. The output can be an integer value, string value or other value.
Syntax:
print(“hi”)
Output:
hi will be displayed on the output screen.
Parameters:
Object Parameter:
The object can be any value like integer, float, etc. This indicates that the object should be printed on the output screen.
Example:
#simple program for print() function
Print(“hi”, ”hello”)
Output:
hi hello
Here hi and hello are considered objects. And they will be printed on the output screen.
Separator(sep) Parameter:
This parameter is used to separate objects or values in the print statement. The default value is empty space(“ ”).
Example:
#program for sep parameter
print(“Hi”, “I am Vijay”, sep=” “)
print(“Hi”, “I am Vijay”, sep=”—“)
Output:
Hi I am Vijay
Hi—I am Vijay
Explanation:
In the first print statement, given sep function and not initialised. So, the default space has been printed between two values.
In the second print statement, the given sep function is initialised by “—“.So, the output is printed like Hi—I am Vijay.
End Parameter:
If we have written two print statements, the second print statement will be printed in a new line. We can also specify what to print at the end using the end parameter.
It may be any characters, numbers, etc. The default value or character is “\n”.
Example 1:
#program without using end parameter in print() function
print(“Hello World”)
print(“I am Vijay”)
Output:
Hello World
I am Vijay
Example 2:
#program using end parameter in print() function
print(“Hello World”, end = ‘ ‘)
print(“I am Vijay”)
Output:
Hello World I am Vijay
Explanation:
As we can see from the above program output, it has printed in two lines, but we have printed the output in the same line using the end parameter by specifying the character as space.
We can also use the end parameter as:
Code:
#program for end parameter in print() function
print(“Hi all”, end = ‘@’)
print(“Good morning”)
Output:
Hi all@Good morning
Explanation:
In this program, instead of space, we have given a special character, “@.” And the first print statements end with the “@” character and the second stars after the “@” character.
File Parameter:
The program must contain an object with the write(string) method to use the file parameter. If we want to print the objects to the output screen, sys.stdout will be used.
Flush parameter:
This parameter is used for flushing the output screen. If it is true, otherwise, it will remain the same. It will remove the buffer from the program. It uses Boolean data type (True or false). The default value for this parameter is false.
Example: (Program for print() function using parameter)
X = “Vijay”
print(“X =”, X, sep = ‘@ ’, end = ‘20’)
print(“ X =”, X, end = ‘###’)
Output:
X =@Vijay 20 X =Vijay
Explanation:
First, we have initialised a string data ‘Vijay’ to a variable X. Then we have used the parameter sep(separator) with the character ‘@’. After that, we used the parameter end (the statement must be ended with the specified character) ‘20’. After 20, the next print statement will be printed, and it will be ended with ‘###’. The output will be as shown above.