One of the most important fundamentals of programming is printing the outputs. Every programming language has its own methods to print the output either to the console or to files. In Python, this process of returning outputs becomes very simple with the print function of Python. In this article, you will be learning all the important aspects of print in Python.
Before moving on, let’s take a look at the contents that are covered over here:
What is print in Python?
print in Python is the standard function used to print the output to the console. The syntax of this function is as follows:
SYNTAX:
print(value1, value2, …, sep = ‘ ‘ , end = ‘ n ‘, file = sys.stdout, flush = False)
The parameters and their descriptions are as follows:
Parameter | Description |
value1, value2, … | The outputs that need to be printed. Can be more than one |
sep | An optional parameter used to specify how you want to separate the objects being printed. The default value of this is one whitespace ( ‘ ‘ ). |
end | An optional parameter used to specify what is to be printed at the end of the output. The default value is ‘n’ |
file | An optional parameter with a write method. The default value is sys.stdout |
flush | An optional parameter used to specify if the output has to be flushed (True) or buffered (False). Its default value is False |
NOTE: All objects will be converted to a string before being returned as the output.
Using print in Python
The print function can be used as follows:
Without optional parameters:
You can make use of the print statement to simply print any output objects as you require. Consider the following example:
EXAMPLE:
print("Using the print function in Python")
OUTPUT: Using the print function in Python
Here, the print function just prints out the given string to the console.
Let us now give more than one value to a single print statement.
EXAMPLE:
a=2019 b='World' print('Hello',a,b)
OUTPUT: Hello 2019 World
As you can see, in the above example, a single print statement prints three different objects. Also, the ‘ + ‘ operator allows concatenation of objects for example:
a='Hi' b='Welcome' print(a+b)
OUTPUT: HiWelcome
Here are few more examples that you can try out:
EXAMPLE:
print('Hello') print('Hello','World') #printting two strings print('Hello'+'World') #concatenating two strings print('Hellon'+'World') #printing with n print('Hello','World',2019) #printing strings along with integers print(2019,'Hello World') print(str(2019)+'Hello World') #concatenating integers with strings (using type conversion) print(34+67) #adding within print
You can also specify any type of separators between each object.
Specifying Separator:
Separator creates a partition between different objects that are present within the print statement. The default value of this attribute is a whitespace character ( ‘ ‘ ). The user can change the value of this operator as and when required.
a='Hello' b='World' print(a,2019,b,sep=',')
OUTPUT: Hello,2019,World
In the above example, different objects are separated by a comma ( , ) rather than a whitespace character in contrast to the previous example.
You can also adjust what you what to print at the end of the output.
Using the end parameter:
The end parameter allows you to configure what you what to print at the end of the output. The default value of this parameter is ‘n’ or the next line character. Let us see what happens when I use two separate print functions to print outputs.
EXAMPLE:
a='Hi' b='Welcome' print(a) print(b)
OUTPUT:
Hi Welcome
Here, the end parameter is not set and hence, the outputs are printed in two separate lines. In case you want to print it them in the same line, you can do as follows:
a='Hi' b='Welcome' print(a,end=' & ') print(b)
OUTPUT: Hi & Welcome
In the above example, the value of the end parameter is ‘ & ‘ as seen between the outputs.
The print statement can also write outputs to a file.
Writing to a file:
The output can be written to a file optionally using the file parameter. In case the file is not present, it creates a new file with that name and writes the output to it. For example:
EXAMPLE:
newfile = open('abc.txt','w') print('Hi Welcome', file = newfile) newfile.close()
OUTPUT: Take a look at the file in the image below:
The flush parameter:
The flush parameter of print in Python allows you to choose buffered or unbuffered output. the default value of this parameter is False, meaning the output will be buffered. In case you set this to be True, the output is unbuffered and this process is usually slower than the former. Take a look at the time taken for default buffered output in the example below:
EXAMPLE:
import time g=open('sample.txt', 'r') a=g.read() s=time.time() print(a,flush=False) e=time.time() print(e-s)
OUTPUT:
The time taken for this to execute is 0.00099 seconds. Now, let’s try to change the value to True.
EXAMPLE:
import time g=open('sample.txt', 'r') a=g.read() s=time.time() print(a,flush=True) e=time.time() print(e-s)
OUTPUT:
The same process takes 0.003 seconds when the output is unbuffered. This is because its easier to transfer the output in chunks rather than printing it in a sequence of characters. Usually all I/Os are buffered. However, this option is convenient when the user needs to flush the entire output in special scenarios.
This brings to the end of this article on “print in Python”. I hope you have understood everything clearly. Make sure you practice as much as possible and revert your experience.
Got a question for us? Please mention it in the comments section of this “print in Python” blog and we will get back to you as soon as possible.
To get in-depth knowledge on Python along with its various applications, you can enroll for live Python online training with 24/7 support and lifetime access.