- The print function in Python is used to display the output of variables: strings, lists, tuples, ranges, etc.
- Before version 3.x of Python, the print was taken as a statement.
- However, from Python 3.x, the print acts as a function.
- That also means you need to enclose the objects in parenthesis. For example,
print (“A String”) print (a_variable) print (list)
Syntax of using Python print function
Following is the syntax for using the print function:
|objects||The object can be strings, lists, tuples, etc.|
|sep||The sep = ‘’ parameter specifies a space between multiple objects. You may use other than space by using this parameter.|
|end||The end=’\n\‘ means in each call, the print function will end at a newline. A demo is shown later in this tutorial, on how you can change that.|
|file=sys.stdout||The file=sys.stdout specifies where the print function should send the output. The default value is stdout that you may change, for example to an external file.|
|flush||In Python 3.3 the flush keyword argument is added that specifies whether to flush or not the output stream. The default value is false.|
See the following examples where I will use the print function with string and int variables along with other data types like tuples, lists, etc.
I will also show you using different parameters of the print function.
An example of displaying simple string by print function
By using the print Python function, a simple string is displayed in this example:
The code for printing the output:
print ("A demo of print function")
Displaying a string variable by Python print
A string variable is created and displayed by using the print function:
str_a = ("Displaying string variable") print (str_a)
Displaying multiple values separated by commas
In the print() function, you may use any number of values/variables that are separated by commas. Each value is separated by space, see the following example:
str_1 = ("Displaying string variable.") str_2 = ("With multiple values.") str_3 = ("Each value is seperated by space.") print (str_1,str_2,str_3)
You can see that three string variables are displayed by using the print function.
Each variable is separated by a comma. In the output, space is added automatically. This is due to the parameter sep = ‘’, as shown in the syntax of the print function.
Displaying numeric variable example
Similarly, you may use the numeric variables in the print function to display its values. Again, you can use a comma with variables to display the arbitrary number of values.
a = 10 b = 20 c = a + b print (c) print ("The value of c =",c)
You can see that the int variable is displayed alone as well as with a string by using print function. A blank space is also added at the second output to display both string and integer values.
The Python print function call ends at newline example
As shown in the syntax of the print function, the default value of ‘end = \n’ i.e. a newline.
That means the print function ends with a newline in each call. To test that, I have created a list of five numbers in this example.
A for loop is used for iteration through the list elements. In each iteration, the current value in the list is displayed by using the print function. See the code and output:
The code for creating and displaying list elements:
prt_ex = [10,20,30,40,50] for x in prt_ex: print ("List item",x)
You see, in each iteration, the value is displayed in a new line. You can change this as shown in the example below.
An example of changing end parameter rather than using the newline
The following code is used for changing the default newline:
#A demo of print function for x in range(5): print ("Range value =", x, end=". ")
An example of displaying tuple by print function
Just like string or int variables, list, etc. the tuple element can be displayed by using the print function. See the following example where a tuple is created with five elements. After that, the print function is used to display its items:
tup_disp = (1,2,3,4,5) print ("The tuple elements:" ,tup_disp)