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

Python print() function

Last Updated : 10 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Image

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:

Image

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
8 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
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
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
4 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
Print number with commas as 1000 separators in Python
In this program, we need to print the output of a given integer in international place value format and put commas at the appropriate place, from the right. Let's see an example of how to print numbers with commas as thousands of separators in Python. Examples Input : 1000000 Output : 1,000,000 Input : 1000 Output : 1,00Method 1: using f-string F-s
2 min read
Print anagrams together in Python using List and Dictionary
Given an array of words, print all anagrams together. Examples: Input: arr = ['cat', 'dog', 'tac', 'god', 'act'] Output: 'cat tac act dog god' This problem has existing solution please refer Anagrams and Given a sequence of words, print all anagrams together links. We will solve this problem in python using List and Dictionary data structures. Appr
2 min read
Ways to print escape characters in Python
Escape characters are characters that are generally used to perform certain tasks and their usage in code directs the compiler to take a suitable action mapped to that character. Example : '\n' --> Leaves a line '\t' --> Leaves a space C/C++ Code # Python code to demonstrate escape character # string ch = &quot;I\nLove\tGeeksforgeeks&
2 min read
Python code to print common characters of two Strings in alphabetical order
Given two strings, print all the common characters in lexicographical order. If there are no common letters, print -1. All letters are lower case. Examples: Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e(2 times), g(1 time), k(1 time) and s(1 time). Hence the lexicogr
2 min read
Python | Print the initials of a name with last name in full
Given a name, print the initials of a name(uppercase) with last name(with first alphabet in uppercase) written in full separated by dots. Examples: Input : geeks for geeks Output : G.F.Geeks Input : mohandas karamchand gandhi Output : M.K.Gandhi A naive approach of this will be to iterate for spaces and print the next letter after every space excep
3 min read
Print first m multiples of n without using any loop in Python
Given n and m, print first m multiples of a m number without using any loops in Python. Examples: Input : n = 2, m = 3 Output : 2 4 6 Input : n = 3, m = 4 Output : 3 6 9 12 We can use range() function in Python to store the multiples in a range. First we store the numbers till m multiples using range() function in an array, and then print the array
3 min read
Program to print all distinct elements of a given integer array in Python | Ordered Dictionary
Given an integer array, print all distinct elements in array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted. Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45} Output: 12, 10, 9, 45, 2 Input: arr[] = {1, 2, 3, 4, 5} Output: 1, 2, 3, 4, 5 Input: arr[] = {1, 1, 1, 1, 1}
2 min read
Python | Print unique rows in a given boolean matrix using Set with tuples
Given a binary matrix, print all unique rows of the given matrix. Order of row printing doesn't matter. Examples: Input: mat = [[0, 1, 0, 0, 1], [1, 0, 1, 1, 0], [0, 1, 0, 0, 1], [1, 1, 1, 0, 0]] Output: (1, 1, 1, 0, 0) (0, 1, 0, 0, 1) (1, 0, 1, 1, 0) We have existing solution for this problem please refer link. We can solve this problem in python
2 min read
Print all sublists of a list in Python
Given a list, print all the sublists of a list. Python provides efficient and elegant ways to generate and manipulate sublists. In this article, we will explore different methods how to print all sublists of a given list. Examples: Input: list = [1, 2, 3] Output: [[], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]Input: [1, 2, 3, 4] Output: [[], [1], [1
3 min read
Python | Print all the common elements of two lists
Given two lists, print all the common elements of two lists. Examples: Input : list1 = [1, 2, 3, 4, 5] list2 = [5, 6, 7, 8, 9] Output : {5} Explanation: The common element of the lists is 5. Input : list1 = [1, 2, 3, 4, 5] list2 = [6, 7, 8, 9] Output : No common elements Explanation: They do not have any elements in common in between them Method 1:
8 min read
Python | Remove and print every third from list until it becomes empty
Given a list of numbers, Your task is to remove and print every third number from a list of numbers until the list becomes empty. Examples: Input : [10, 20, 30, 40, 50, 60, 70, 80, 90] Output : 30 60 90 40 80 50 20 70 10 Explanation: The first third element encountered is 30, after 30 we start the count from 40 for the next third element which is 6
4 min read
Python | Program to print duplicates from a list of integers
Given a list of integers with duplicate elements in it. The task is to generate another list, which contains only the duplicate elements. In simple words, the new list should contain elements that appear as more than one. Examples: Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]Input :
5 min read
Python | Print all string combination from given numbers
Given an integer N as input, the task is to print the all the string combination from it in lexicographical order. Examples: Input : 191 Output : aia sa Explanation: The Possible String digit are 1, 9 and 1 --> aia 19 and 1 --> sa Input : 1119 Output : aaai aas aki kai ks Approach: Get the String and find all its combination list in the given
2 min read
Print first n distinct permutations of string using itertools in Python
Given a string with duplicate characters allowed, print first n permutations of given string such that no permutation is repeated. Examples: Input : string = "abcab", n = 10 Output : aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb Input : string = "okok", n = 4 Output : kkoo koko kook okko Approach: Python provides an inbuilt method to
2 min read
Python - Ways to print longest consecutive list without considering duplicates element
Given, a list of Numbers, the task is to print the longest consecutive (Strictly) list, without considering duplicate elements. If there is more than one answer, print anyone. These type of problem are quite common while working on some web development projects in Django or flask. Below are some ways to solve the above task. Note : If there are mul
4 min read
Print objects of a class in Python
An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object.
2 min read
Program to print a doormat pattern having a string written in the center in Python
Given the number of rows R and the number of columns C, the task is to print a doormat pattern as shown in the examples below. The number of columns in the mat's design must be 3 times the number of rows in the design. The doormat should have GeeksforGeeks printed in the center. The width of the mat is equal to the number of columns in the pattern.
2 min read
Pretty Print JSON in Python
JSON is a javascript notation of storing and fetching the data. Data is usually stored in JSON, XML or in some other database. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. Note: For more information, refer to Read, Write and Parse JSON using Python Pretty Print JSON Whenever d
2 min read
How to print to stderr and stdout in Python?
In Python, whenever we use print() the text is written to Python’s sys.stdout, whenever input() is used, it comes from sys.stdin, and whenever exceptions occur it is written to sys.stderr. We can redirect the output of our code to a file other than stdout. But you may be wondering why one should do this? The reason can be to keep a log of your code
3 min read
How to print Odia Characters and Numbers using Python?
Odia(ଓଡ଼ିଆ) is an Indo-Aryan language spoken in the Indian state of Odisha. The Odia Script is developed from the Kalinga alphabet, one of the many descendants of the Brahmi script of ancient India. The earliest known example of Odia language, in the Kalinga script, dates from 1051. Vowels: ଅ ଆ ଇ ଈ
2 min read
How to print exception stack trace in Python?
Prerequisite: Python Traceback To print stack trace for an exception the suspicious code will be kept in the try block and except block will be employed to handle the exception generated. Here we will be printing the stack trace to handle the exception generated. The printing stack trace for an exception helps in understanding the error and what we
3 min read
Practice Tags :
three90RightbarBannerImg