The print() function is actually the backbone of computer languages. This function helps to display the output or desired message on the user’s screen or wherever the user wants on this output device. This message returned from the print() function can either be a data type such as a string, or it can be an object or even an object which has undergone some conversions before coming out as the output. In this article, we will be discussing the importance of the print() function in python along with an example of writing the print() function in python. We will also discuss the python print() parameters, their uses, and some python code examples for better understanding. We will also talk about string literal and its examples.
As we have discussed the importance of the print() function in computer programming, it plays the same role in python as well. This function helps to print the desired output on the screen of the user or his device. The output can also be displayed in the text file. It is not always mandatory that arguments need to be passed in the print() function. The only requirement is that it needs to have an empty parenthesis whc=ich will execute the function at the end of the python program. The process will happen by calling the function by name.
Below is the basic python program using the print() function to display a message:
message = 'Welcome to HKR training'
print(message)
Output:
Welcome to HKR training
print(*object(s), sep(separated)=' ', end=, file=sys.stdout, flush(Flush)=False)
Let us see the meaning of the parameters written in the syntax above:
Become a python Certified professional by learning this HKR Python Training !
An object will be first converted to a string before giving an output message. The * before the object states that it consists of more than 1 value.
This is actually a separator. It comes into play to separate the objects when more than 1 object values are present in the code. The default value always remains ‘ ‘.
This is to print the end of the statement. This is not a mandatory field. The default value for the end is ‘\n’
This parameter states an object with the write method, actually string. If not specified, the default value which will be displayed on the screen is sys.stdout
The input-output python statements are generally used in bits or chunks. Here, the flush plays a major role in deciding whether the statement needs buffering or not. If the flush value is false, the stream is not flushed however, if the output value is true, the stream gets flushed forcefully. The default value for flush always remains false.
Now, let us understand the above parameters and have a look at some examples below:
Below is a python code to display the output using a print() function with multiple object values:
print("Welcome to HKR Training")
x = 5
print("The value of x =", x)
y = x
print('The value of x =', x, '= the value of y')
Output:
Welcome to HKR Training
The value of x = 5
The value of x = 5 = the value of y
In the python code above,
Below is a python example to demonstrate the print() function with more than one values:
print("Hello!", "Welcome to HKR Training")
Output:
Hello! Welcome to HKR Training
>
Below is a python program to display a tuple using the print() function:
z = ("Red", "Blue", "Green")
print(z)
Output:
('Red', 'Blue', 'Green')
>
Below is a python code to display the output with multiple values along with the separator:
print("Hello!", "Welcome to HKR Training", sep="---")
Output:
Hello!---Welcome to HKR Training
>
Below is a python code to display the output using a print() function with a separator along with the end value:
z = 5
print("The value of z is =", z, sep='0000', end='\n\n')
print("The value of z is =", z, sep='0', end='')
Output:
The value of z is =00005
The value of z is =05
Below is a python code to display the file parameter:
source_File = open('print.txt', 'x')
print('NICE AND BEAUTIFUL FILE', file = source_File)
source_File.close()
Output
This program will try to open the file mentioned by the user on his device in writing mode. In case, there is no file as print.txt present, then it will create one file and open it in writing mode. Finally, the file.close() method will close the file.
Top 50 frequently asked Python interview Question and answers !
In the print() statement in python, string literals are actually used for the formatting as well as designing of the specific string on how it will appear on the display screen when printed.
Below is a python program to understand how string literals work using a print() function in python:
print("Hello! \nYou are welcome to HKR Training")
Output:
Hello!
You are welcome to HKR Training
If you want to Explore more about Python? then read our updated article - Python Tutorial.
Conclusion
In this article, we have discussed the use of the python print() function in computer programming. The print() function is actually used to display the output or desired message on the user’s screen or wherever the user wants on this output device. This message returned from the print() function can either be a data type such as a string, or it can be an object. We have also discussed the print() parameters in python along with their examples and string literals. Hence, the main idea of this article is to understand the use of the print() function in python.
The print() function in python can take up multiple values and objects and can display them as a single statement on the output screen.
This is a newline character that shifts the statement into a new line.
This is a character that helps in shifting the cursor to the beginning of the python code.
A user can print the output in python using this syntax:
print(*object(s), sep(separated)=' ', end=, file=sys.stdout, flush(Flush)=False)
Related Articles
Batch starts on 26th Sep 2023, Weekday batch
Batch starts on 30th Sep 2023, Weekend batch
Batch starts on 4th Oct 2023, Weekday batch