Python Print Function

In this article, we will learn how a Python Print Function works. The syntax of the Python print function is given in here and you can learn about the parameters of Python print function as well.

Introduction
 
In this article, we will cover the following topics.
  • Overview of print() function in Python
  • The syntax of the print() function
  • Parameters which can be passed into the print() function
  • Understanding all the parameters of the Python print() function with example
  • Writing files using the print() function
Python print() function
 
Python print function is mainly used to print the objects to the standard output screen or to a stream file.
  
Syntax of the python print() function
  1. print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)  
Parameters of the print() function
 
As you can see in the syntax, there are five parameters which we can pass to the print() function.
  • *Object
    Whatever object you want to print on the screen or to the file. Here, you can see the print() function prefixed with the asterisk (*) which says that you can pass more than one object.

  • sep
    sep means separator whose default value you can see one space.

  • end
    After every print statement, you will define in the end parameter what you want to print. The default value of the print statements is Newline (\n)

  • file
    By using the file parameter, you can write your output into the file stream. By default, its value is sys.stdout. This means the output will be printed on the system's standard output stream, i.e., on the console.

  • flush
    It takes either True or False. When the value is true, the stream will be forcefully flushed. By default, its value is False.
Example 
 
Simple print() function
  1. '''''-------------Example 1-------------'''  
  2.   
  3. # Passing string object    
  4. print("Hello C# Corner")  
Output
 
Python Print Function 
 
Example 
 
Multiple objects passing in the print() function without a separator.
  1. # Passing multiple object with space   
  2. # Because "sep" parameters default values is space (" ")  
  3. print(2,3,4# It will print 2 3 4  
Output
 
Python Print Function
 
Example 
 
Using "sep" parameter.
  1. # Changing the value of the "sep" parameter  
  2. print("C","Sharp","Corner",sep="-")  
Output
 
Python Print Function 
 
Example 
 
Understanding the "end" parameter.
  1. '''''-------------Example 1-------------'''  
  2.   
  3. # Print without any object  
  4. print("Hello")  
  5. print() # There is no object but default value of the end parameter is '\n'  
  6. print("Welcome to C# Corner")  
  7.   
  8. '''''-------------Example 2-------------'''  
  9.   
  10. print("Hello",end='! '# It will print Hello!   
  11. print("C",end='-'# It will print C-  
  12. print("SharpCorner",end='.'#It will SharpCorner.  
  13. # So the Complete output will be Hello! C-SharpCorner  
Output
 
Python Print Function 
 
Example 
 
Writing file using print().
  1. # Writing file using print function  
  2. myfile = open('csharpcorner.txt''w')  
  3. print('Welcome to https://www.c-sharpcorner.com', file = myfile)  
  4. myfile.close()  
Output
 
The above code will create one csharpcorner.txt file and will print the function's output to that file.
 
What Python print() function returns?
 
Python print() function always returns "None".
 
Example
 
The returning value from the print function.
  1. result=print("Hello"#return value will be save into result  
  2. print(result) # It will print none because print returns None  
Output 
 
Python Print Function