Hello learners. In this tutorial we are going to learn more about Python print function. In our last tutorial we learned about Python float function.
Table of Contents
Almost all of our previous tutorial contains the Python
print() function. But we did not discussed about python print function to the fullest. Now we will learn it. At first we should know about the basic structure of python print function. That is given below;
If you read our tutorial on Python Functions and arguments, then you should probably had the idea about the above function.
values receives a list of undefined variables. So, all the comma separated values will goes under the list values. So if you add more elements separated by comma, you will get a output where all the values are put together separated by space. The following example will guide you about the simple python print function usage.
Copy# initialize 1st variable var1 = 1 # initialize 2nd variable var2 = 'string-2' # initialize 3rd variable var3 = float(23.42) print(var1, var2, var3)
The output of the following code will be.
Copy1 string-2 23.42
So, as many item you want to print, just put them together as arguments.
Using sep Keyword in python print function
If see the example of the previous section, you will notice that that variables are separated with a space. But you can customize it to your own style.
Suppose in the previous code, you want to separate the values using underscore(_). Then you should pass underscore as the value of sep keyword. The following function will illustrate you the idea of using python print sep keyword.
Copy# initiaze 1st variable var1 = 1 # initialize 2nd variable var2 = 'string-2' # initialize 3rd variable var3 = float(23.42) print(var1, var2, var3, sep='_')
And you will get your desired output like this.
Python print end keyword
The end key of print function will set the string that needs to be appended when printing is done.
By default the end key is set by newline character. So after finishing printing all the variables, a newline character is appended. Hence, we get the output of each print statement in different line. But we will now overwrite the newline character by a hyphen(-) at the end of the print statement. See the following example.
Copy# initialize a list initList = ['camel', 'case', 'stop'] # print each words using loop print('Printing using default print function') for item in initList: print(item) # default print function. newline is appended after each item. print() # another newline # print each words using modified print function print('Printing using modified print function') for item in initList: print(item, end='-')
And you will get outputs like the following
CopyPrinting using default print function camel case stop Printing using modified print function camel-case-stop-
Python print to file
In this section we will learn about
file keyword. Actually file keyword is used for extracting output to a specified file. If you read our previous tutorial Python file operation, then you should know about basic file operation.
So, you have to open a file in writable mode first, then use the file pointer as the value of file keyword in print() function. See the following code to understand the python print file usage.
Copy# open a file in writable mood fi = open('output.txt', 'w') # initialize a list initList = ['camel', 'case', 'stop'] # print each words using loop print('Printing using default print function') for item in initList: print(item, file=fi) # use file keyword print(file=fi) # print each words using modified print function print('Printing using modified print function') for item in initList: print(item, end='-', file=fi) # use file keyword # close the file fi.close()
And you will get the same output as the previous example in an output text file.
That’s all about Python print. Hope that you understood it well. For any further query, feel free to use the comment section. Best of luck.