Python print() Method Tutorial with Examples

Python programming language provides the print() method which is used to output given data. This data can be in different types like integer, string, list, tuple, etc. In Python2 the method was a keyword-like print where there was no parenthesis but with Python3 it is standardized and converted into a function where data is accepted as parameters.

print() Method Syntax

print() method has a bit complex syntax where you can use single or 10 or more parameters in order to print given data or object into the standard output.

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
  • *objects are the single or multiple objects, variable, or data which will be printed outputted. *object can be a string variable or a class or multiple integers. *objects are mandatory and should be provided while using print() method.
  • sep=’ ‘ is the separator parameter which is optional. It is used to provide separator character which can be a letter, number, or sign which will be put between multiple objects*. By default space or ‘ ‘ is used separator if not specified.
  • end=’\n’ is added at the end of the given output. This parameter is optional and if not provided the end of line or ‘\n’ will be used.
  • file=sys.stdout is an optional parameter where the print output direction specified. By default, the standard output is used.
  • flush=False parameter is optional and by default False. If enabled with True the output will be printed immediately without buffering.

According to the previous explanations the print() function can be used in different ways like below.


print("Test" , separator="||")

print("Test" , separator="||")

print("Test" , separator="||" , separator="||")

Python Print String and Numbers

We will start with simple examples where we will print different strings and values. The string values also called literals. As we can see the print method prints the given single or multi-character string and jumps to the new line automatically. The print() method only prints strings but it converts an integer into a string automatically. If we provide numbers as strings like “99” it will be printed directly to the standard output. If it is an integer like 99, first it will be converted into a string which is “99” and then printed.

print("I am wisetut")
# I am wisetut

print('I am wisetut')
# I am wisetut

# 99

# 99

# 99
Print String and Numbers

Python Print Variables, Lists, Tuples

We can use the print() method in order to print variables. These variables can be in different types like integer, string, tuple, class, list, etc.

a = 12

b = "I am wisetut"

c = [ 1 , 2 , 3 , 4 ]

d = [ 1 , 2 , "Wisetut"]

e = ( 1 , 2 , 3 )







Print Variables

We can see them when printing lists, tuples their signs like [], (), and a comma is printed too in order to express these variable and data structure types.

Python Print List

We will examine how to print the list type complete or some of the list items. We can use list range specifiers.

a = [ 1 , 2 , 3 , 4, "Wisetut" ]






Print List

Python Print String and Contaneation

Strings are the most popular types used to print to the console, terminal, standard output.

a = ""

b = "Hello World"

c = ""





Print String and Contaneation

We can see from the output that single or multiple strings can be printed by using a single print() method. If we specify multiple strings as different objects or parameters the separator will be added for each parameter where space is the default separator. If we concatenate multiple strings there will be no separator and each string will be joined together without an extra space character. We also see that two or more strings can be concatenated and added together in different ways. Even we will examine the separator later in this tutorial lets provide an example for a different separators.

a = ""

b = "Hello World"

c = ""

print(a , b , sep = ",,,")

print(a , b , c , sep = "|||")
Python Print String Separator

Python Print Separator

If we want to print multiple data or objects print() method provides the separator where space character is the default separator. A separator will be put between objects. We can change this separator even remove by explicitly specifying none.

a = ""

b = "Hello World"

c = ""

print(a , b , c , sep = ",,,")

#Use pipe as separator
print(a , b , c , sep = "|||")

#Remove space separator and join multiple object output
print(a , b , c , sep = "")

#Use new line as separator
print(a , b , c , sep = "\n")
Python Print Separator

We can use sep=”” parameter where the print() objects outputted without space or another separator. Also, we can provide different control characters as separators like sep=”\n” or newline where each object will be outputted into a new line.

Python Print End Character

After the output is completed the print() method prints an end character which is “\n” by default. This end character can be explicitly specified with the end=”\n” parameter whatever we want. The end character can be a number or multiple new lines there is no restriction. The end character will be added to the end of the output, not between all objects of the print() method.

a = ""

b = "Hello World"

c = ""

print( a , end="\n\n\n" )

print( a , b , c ,  end="\n\n\n" )

print( a , b , c ,  end="999" )

print( a , b , c ,  end="XYZ" )

print( a , b , c ,  end="" )
Python Print End Character

We can use the end character for single or multiple objects while printing. Also, end character can be consist of multiple characters where more than a single end of the line can be used too. We can set end character as numbers and letters where the default end of the line will be not used and given numbers and letters will be used. We can even remove the end character with an empty string like end=”” where there will be no character even the end of the line at the end of the output.

Python Print Output Direction

In most of cases print() method is used to print output into the standard output which is generally a terminal, shell or console. But print() method output direction can be changed by using the file parameter where the default is sys.stdout which is standard output. We can specify a file or different output mechanism like the standard error with the file parameter. In order to set standard error, the sys module should be imported.

import sys

a = ""

print( a , file = sys.stdout )

print( a , file = sys.stderr )

f = open( "example.txt" , mode = "w")
print( a , file = f)
Python Print Output Direction

Python Print Into A File

Well you may ask that “How can I print output into a file with the print() method in Python?”. print() method provides the file parameter which is explained in the previous part where we can open the file and provide the file parameter. In order to write into a file, the file should be opened with a writeable mode like w or w+, etc. Below we will print the output into the file named example.txt.

a = ""

b = ""

#Open the text file named example.txt
f = open( "example.txt" , mode = "w")

#Print a and b variables into the file named example.txt
print( a , b , file = f)

Python Print Emojis

Emojis are very popular in recent times and Python print() method can be also used to print emojis. Emojis can be printed into the shell or terminal if it has graphical user interface in a elegant way with beautiful graphics. Emojis are expressed with Unicode or CLDR shortcode. Below we will print some emojis to the standard output. Emoji codes are provided via the emoji module in Python.

#grinning face

#grinning face with big eyes	

#grinning face with smiling eyes

#beaming face with smiling eyes
Python Print Emojis

Leave a Comment