- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python print() Function
The print() function or statement in Python is I think the most used statement in my perspective. Because every time we need to produce or print some output, the print() function is required. This article teaches you everything about this function.
Definition of print() Function
The print() function is used to produce output. For example:
print("Hey!\nWelcome to fresherearth.com")
The above program produces exactly same output as shown in the snapshot given below:
Syntax of print() Function
The syntax to use print() function is:
print(value(s), sep=' ', end='\n', file=sys.stdout, flush=False)
Note: Except value(s), all other parameters are optional. Even value(s) is also optional. That is, print() doesn't produce any error if you doesn't provide any parameter to it. But if you doesn't provide any value/expression, then the output will be nothing or blank for that print() statement.
Note: Normally we don't need the last two parameters. The explanation of these two parameters are also given here.
All Parameters of print() Function
In above syntax of print(). Here are the description of all the parameters one by one.
The value(s)/expression(s) Parameter
This is the most used parameter. Any number of value/expression can be passed. That is, the expression passed in print() is not limited to only one. That is, you can pass any number of expressions to print() where expression must be separated by a comma as shown in the program given below:
print("Hey!", "\nWelcome to fresherearth.com")
produces the same output as of very first program of this article.
Note: The print() function converts all the expressions passed in it, into a string before writing or printing the things to the output screen.
The sep= Parameter
Now the second parameter is sep=' '. It is used to separate expression using manually assigned character or combination of characters. To learn more about it, refer to sep parameter in Python. If you doesn't use or provide this parameter to print(). Then by default, all expression gets separated by a single space.
The end= Parameter
The third parameter is end='\n'. It is used to skip insertion of an automatic newline using print() on the output screen. To learn more about it, refer to end parameter in Python. If you doesn't use it, then after each print() statement, a newline automatically gets inserted on output screen.
The file= Parameter
The fourth parameter is file=sys.stdout. This parameter is used to write some content to a file instead of writing on output screen. If you doesn't use this parameter, then the sys.stdout automatically gets used, that prints objects or expressions, on the output screen. Here is an example program that uses file= parameter:
with open('fresherearth.txt', mode='w') as file_object: print('Hey, Welcome', file=file_object)
creates a file fresherearth.txt inside the current directory and writes Hey, Welcome string to that file. If you open the folder, where the source code of above program is saved. Then you see a file fresherearth.txt is available. And if you open that file, it contains Hey, Welcome as content. That is, if you need to write all the expression of print() to a file instead of writing to output screen, you need to use file parameter. Here is the snapshot of the folder where the file is created, along with opened file fresherearth.txt:
Note: To learn about file handling in Python, refer to the separate tutorial on it.
The flush= Parameter
The last or the fifth parameter is flush. It is used to flush the stream. The default value of this parameter is False. Specifying True to it, means the output gets written as a sequence of characters one by one. Writing one character at a time is a slow process. Therefore we do not need it until a special requirement occur where we need to write one character at a time.
print() Function Example
Here is an example of print() function that uses all the parameters as shown in its syntax given above:
import sys print("Hello", 123, "How are you?", sep="\n", end="\n", file=sys.stdout, flush=False)
This program produces exactly the same output as shown in the snapshot given below:
Note: Except sep=, all parameter is assigned with its default values in above program.
I've shown you the complete output screen produced by above program. The IDE used is PyCharm. Therefore, if you run the above program in PyCharm, then exactly same output you'll see.
The reason behind showing the complete output is, to mainly show you the use of sep and end. As you can see, each expression say Hello, 123 and How are you? gets separated by a newline using the sep, because sep works on expression for the current print() function where it is available.
Whereas the end doesn't works on expressions, rather it works on the complete current print() function where it is available. Its effect applied on next print(). Therefore, after complete printing of expressions, the next output gets moved to a newline as shown in the snapshot given below:
But in place of end="\n", if you use end="" in above program, then here is the output you'll see:
« Previous Function Next Function »