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.
hi will be displayed on the output screen.
The object can be any value like integer, float, etc. This indicates that the object should be printed on the output screen.
#simple program for print() function Print(“hi”, ”hello”)
Here hi and hello are considered objects. And they will be printed on the output screen.
This parameter is used to separate objects or values in the print statement. The default value is empty space(“ ”).
#program for sep parameter print(“Hi”, “I am Vijay”, sep=” “) print(“Hi”, “I am Vijay”, sep=”—“)
Hi I am Vijay Hi—I am Vijay
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.
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”.
#program without using end parameter in print() function print(“Hello World”) print(“I am Vijay”)
Hello World I am Vijay
#program using end parameter in print() function print(“Hello World”, end = ‘ ‘) print(“I am Vijay”)
Hello World I am Vijay
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:
#program for end parameter in print() function print(“Hi all”, end = ‘@’) print(“Good morning”)
Hi [email protected] morning
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.
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.
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 = ‘###’)
X [email protected] 20 X =Vijay
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.