The Wayback Machine - https://web.archive.org/web/20240710111254/https://www.geeksforgeeks.org/python-print-function/
Open In App

Python print() function

Last Updated : 10 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The python print() function as the name suggests is used to print a python object(s) in Python as standard output.

Syntax: print(object(s), sep, end, file, flush)

Parameters:

  • Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into strings.
  • sep: It is an optional parameter used to define the separation among different objects to be printed. By default an empty string(“”) is used as a separator.
  • end: It is an optional parameter used to set the string that is to be printed at the end. The default value for this is set as line feed(“\n”).
  • file: It is an optional parameter used when writing on or over a file. By default,, it is set to produce standard output as part of sys.stdout.
  • flush: It is an optional boolean parameter to set either a flushed or buffered output. If set True, it takes flushed else it takes buffered. By default, it is set to False.

Example 1: Printing python objects

Python3




# sample python objects
list = [1,2,3]
tuple = ("A","B")
string = "Geeksforgeeks"
 
# printing the objects
print(list,tuple,string)


Output:

[1, 2, 3] ('A', 'B') Geeksforgeeks

Example 2: Printing objects with a separator

Python3




# sample python objects
list = [1,2,3]
tuple = ("A","B")
string = "Geeksforgeeks"
 
# printing the objects
print(list,tuple,string, sep="<<..>>")


Output:

[1, 2, 3]<<..>>('A', 'B')<<..>>Geeksforgeeks

Example 3: Specifying the string to be printed at the end

Python3




# sample python objects
list = [1,2,3]
tuple = ("A","B")
string = "Geeksforgeeks"
 
# printing the objects
print(list,tuple,string, end="<<..>>")


Output:

[1, 2, 3] ('A', 'B') Geeksforgeeks<<..>>

Example 4: Printing and Reading contents of an external file

For this, we will also be using the Python open() function and then print its contents. We already have the following text file saved in our system with the name geeksforgeeks.txt.

To read and print this content we will use the below code:

Python3




# open and read the file
 my_file = open("geeksforgeeks.txt","r")
   
# print the contents of the file
print(my_file.read())


Output:

Example 5: Printing to sys.stderr

Python3




# Python code for printing to stderr
 
# importing the package
# for sys.stderr
import sys
 
# variables
Company = "Geeksforgeeks.org"
Location = "Noida"
Email = "contact@geeksforgeeks.org"
 
# print to stderr
print(Company, Location, Email, file=sys.stderr)


Output:

Geeksofrgeeks.org Noida contact@geeksforgeeks.org


Previous Article
Next Article

Similar Reads

Print powers using Anonymous Function in Python
Prerequisite : Anonymous function In the program below, we have used anonymous (lambda) function inside the map() built-in function to find the powers of 2. In Python, anonymous function is defined without a name. While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword. Hence, ano
2 min read
Decorator to print Function call details in Python
Decorators in Python are the design pattern that allows the users to add new functionalities to an existing object without the need to modify its structure. Decorators are generally called before defining a function the user wants to decorate. Example: # defining a decorator def hello_decorator(func): # inner1 is a Wrapper function in # which the a
3 min read
Print Function in C, C++, and Python
In this article, the task is to observe the behavior of the print function in C, C++, and Python. Print function is used to display content on the screen. Approach: Some characters are stored in integer value inside printf function.Printing the value as well as the count of the characters.Illustrating the different use of printf(). Below is the C p
2 min read
file parameter of Python's print() Function
print() function in Python3 supports a ‘file‘ argument, which specifies where the function should write a given object(s) to. If not specified explicitly, it is sys.stdout by default. It serves two essential purposes: Print to STDERRPrint to external fileNote: The 'file' parameter is found only in Python 3.x or later. Printing to STDERRSpecify the
5 min read
Python | Output using print() function
Python print() function prints the message to the screen or any other standard output device. In this article, we will cover about print() function in Python as well as it's various operations. Python print() Function Syntax Syntax : print(value(s), sep= ' ', end = '\n', file=file, flush=flush) Parameters:  value(s): Any value, and as many as you l
7 min read
Wand function() function in Python
function() function is similar to evaluate function. In function() function pixel channels can be manipulated by applies a multi-argument function to pixel channels. Following are the list of FUNCTION_TYPES in Wand: 'undefined''arcsin''arctan''polynomial''sinusoid' Syntax : wand.image.function(function, arguments, channel) Parameters : ParameterInp
1 min read
Returning a function from a function - Python
Functions in Python are first-class objects. First-class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. Properties of first-class functions: A function is an instance of the Object type.You can store the function in a variable.You can pass the functi
3 min read
Python - Call function from another function
Prerequisite: Functions in Python In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples.  What is Calling a Function
5 min read
Python end parameter in print()
By default Python's print() function ends with a newline. A programmer with C/C++ background may wonder how to print without a newline. Python's print() function comes with a parameter called 'end'. By default, the value of this parameter is '\n', i.e. the new line character. Example 1: Here, we can end a print statement with any character/string u
2 min read
Print Colors in Python terminal
In this article, we will cover how to print colored text in Python using several methods to output colored text to the terminal in Python. The most common ways to do this are using: Using colorama ModuleUsing termcolor ModuleUsing ANSI Code in PythonMethod 1: Print Color Text using colorama Module Colorama module is a Cross-platform printing of col
3 min read
Practice Tags :
three90RightbarBannerImg