Python print() function
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 objectslist = [1,2,3]tuple = ("A","B")string = "Geeksforgeeks"# printing the objectsprint(list,tuple,string) |
Output:
[1, 2, 3] ('A', 'B') Geeksforgeeks
Example 2: Printing objects with a separator
Python3
# sample python objectslist = [1,2,3]tuple = ("A","B")string = "Geeksforgeeks"# printing the objectsprint(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 objectslist = [1,2,3]tuple = ("A","B")string = "Geeksforgeeks"# printing the objectsprint(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 fileprint(my_file.read()) |
Output:
Example 5: Printing to sys.stderr
Python3
# Python code for printing to stderr# importing the package# for sys.stderrimport sys # variablesCompany = "Geeksforgeeks.org"Location = "Noida"Email = "contact@geeksforgeeks.org"# print to stderrprint(Company, Location, Email, file=sys.stderr) |
Output:
Geeksofrgeeks.org Noida contact@geeksforgeeks.org
Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


