Python print Function

The python print function is one of the built-in function used to print the given value as an output. If you want something to be written o the screen or output device, then you have to use this Python print function.

In this section, we explain to you how to use this Python print function with multiple examples. The basic syntax behind this Python print function is

print(value,……., sep = ‘ ’, ends = ‘\n’, file = sys.stdout, flush = False)

Let me see the detailed information behind this Python print function arguments:

  • Value: This Python print function display this value to output or writes it to a file using file argument.
  • sep: It’s an optional argument. If you specify any string then that string value inserted between values. By default, this value is space.
  • end: It’s an optional argument of Python print function. If you have given any string, then that string is appended after the last value. The default value is a newline (\n).
  • file: This is an optional argument. You can use this to display the output in a text file or something like that. By default, it uses sys.stdout (standard output)
  • Flush: Change it to true if you want to flush the stream forcibly.

Python print examples

In this example, we are using the Python print function to display Hello world as an output.

print("Hello World")
print('Hello World!')
Hello World
Hello World!

In Python print function, you can use either a double quotation or a single quotation or combination of both to retune the string as an output.

Here, we are displaying two individual strings separated by comma. As we said earlier, it uses space as the default separator.

print("Learn",  'Free Programming')
print("Learn",  'Free', 'Programming')
Learn Free Programming
Learn Free Programming

Use the below Python print function statements to retunr an empty line because, by default, it uses a newline as the argument value.

print(' ')


I think you might not have noticed the empty spaces. So, let me use the string in-between those statements. This Python print function example stamps two empty lines, Hello world, an empty line, Learn Python Programming, a new line, and Hi.

print(' ')
print("Hello World")
print('Learn',  'Programming')
print(' ')

Hello World

Learn Programming

print variables

When you are calling a variable or displaying the variable value, you have to call its name without any quotations. Here, we declared a number and assigned 100. Next, we called that number without quotation and with a quotation.

The first Python print function statement is calling the variable number value. The second one is stamping the word number. So, please be careful while calling the variable values or list values, etc.

number = 100
name = 'Tutorial Gateway'
print('Number = ', number)
print('Name = ', name)
Python Print 5

Python print List, Tuple, Set and Dictionary

Using this Python print function to print the items in Lists, Tuples, Sets, and Dictionary. As you can see from the below, we created two lists, two tuples, three sets, and 1 dictionary. Next, we used it to display them.

If you want to access each item individually or to perform operations on each item, then use For Loop along with range Function.

numbers_list = [10, 20, 30, 40, 50]
fruits_list = ['apple', 'cherry', 'mango', 'kiwi']
numbers_tuple = (15, 25, 35, 45, 55)
fruits_tuple = ('apple', 'cherry', 'kiwi', 'mango')
numbers_Set = {1, 2, 3, 4, 5}
fruits_Set = {'apple', 'kiwi', 'banana', 'orange'}
mixed_Set = {'banana', 1, 2, (1, 2, 3)}
myDict = {'name': 'Kevin', 'age': 25, 'job': 'Developer'}

Python output of printing list, tuple, and set items

[10, 20, 30, 40, 50]
['apple', 'cherry', 'mango', 'kiwi']
(15, 25, 35, 45, 55)
('apple', 'cherry', 'kiwi', 'mango')
{1, 2, 3, 4, 5}
{'orange', 'banana', 'apple', 'kiwi'}
{1, 2, (1, 2, 3), 'banana'}
{'name': 'Kevin', 'age': 25, 'job': 'Developer'}

Python print sep example

By default, this function uses the empty space as the separator. However, you can override this with any of your own string. The third statement in this Python print sep example uses * as the separator between two strings Hello and World. The next statement uses — as the separator.

print("A",  "B", "C", sep = "")
print("A",  "B", "C", sep = " ")
print("Hello",  "World", sep = "*")
print('Learn',  'Free Programming', sep = "---")
print('Learn',  'Free', 'Programming', sep = " ")
print('Learn',  'Free', 'Programming', sep = " # ")
print("Hello",  "World", sep = " AAA ")
print("Hello",  "World", sep = ',,,...,,,')
Learn---Free Programming
Learn Free Programming
Learn # Free # Programming
Hello AAA World

Python print end example

How to use print function without a new line?. By default, the range function uses the new line as the end argument value. However, you can override it. The first two statements in this python print end example displays the strings in two different lines.

In the fourth statement, we used the end argument with space as the value and the fifth statement with . as the argument value. It means, after stamping the Welcome to, space appended. Next, the fifth statement display the Tutorial Gateway after that space, and the statement ends with.

print('Welcome to')
print('Tutorial Gateway')
print('Welcome to', end = ' ')
print('Tutorial Gateway', end = '.')
Welcome to
Tutorial Gateway
Welcome to Tutorial Gateway.

Python Print end and sep arguments

In this Python print function arguments example, we are using both the sep argument and the end argument. From the below, (10, 20, 30, sep = ‘ @ ‘, end = ‘ **** ‘) returns 10 20 30 separated by @ symbol and ends with ****. Similarly, (‘A’, ‘B’, ‘C’, sep = ‘ , ‘, end = ‘ ##### ‘) statements returns A B C separated by comma and ends with #####

print(10, 20, 30)
print('A', 'B', 'C')
print(10, 20, 30, sep = ' , ')
print('A', 'B', 'C', sep = ', ')
print(10, 20, 30, sep = ' @ ')
print('A', 'B', 'C', sep = ' -- ')
print(10, 20, 30, sep = ' @ ', end = ' **** ')
print('A', 'B', 'C', sep = '--', end = ' . ')
print('A', 'B', 'C', sep = ' , ', end = '  #####  ')
print(10, 20, 30, sep = ' @ ', end = ' !!! ')
10 20 30
10 , 20 , 30
A, B, C
10 @ 20 @ 30
A -- B -- C
10 @ 20 @ 30 **** A--B--C . 
A , B , C  #####  10 @ 20 @ 30 !!! 

It is another example of the function arguments. Here also, we used this along with the separator argument and the end argument.

For example, (‘Number = ‘, num, sep = ‘0000’, end = ‘?\n\n\n’) display number separated by 0000 and ends with ? and three new lines.

num = 1234
print('Number = ', num, sep = '****', end = '!')
print('Number = ', num, sep = '0000', end = '.')
print('Number = ', num, sep = '0000', end = '?\n\n\n')
print('Number = ', num, sep = '0000', end = '@\n')
Number = ****1234!
Number = 00001234.
Number = 00001234?

Number = [email protected]

Python For Loop print Example

Use this print function inside the For Loop along with Range Function. By this, you can display the items within a given range — the first statement returns numbers from 1 to 9.

for i in range(1, 10):
for i in range(1, 10):
    print(i, end = "   ")
for i in range(1, 10):
    print(i, end = " , ")
1   2   3   4   5   6   7   8   9   
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 

Python print String Format

We are using the print function along with conversion types. Within the first statement, we used two %s in between a string follows by a tuple with variables. It means, python replaces those two %s items with those tuple values.

Here, it uses the same order that we specified. I mean, first, %s is replaced by a person variable, and the second %s replaced by name variable value. Here, (person +’ is working at ‘+ name) statement is to concat three items.

name = 'Tutorial Gateway'
person = 'suresh'
year = 2019
print('%s is working at %s' %(person, name))
print('Copyright %s at %d' %(name, year))
print(person, ' is working at ', name)
print(person + ' is working at ' + name)
print('Copyright ', name, ' at ', year)

print function and string formatting output

suresh is working at Tutorial Gateway
Copyright Tutorial Gateway at 2019
suresh  is working at  Tutorial Gateway
suresh is working at Tutorial Gateway
Copyright  Tutorial Gateway  at  2019

Python print format Example

It is an example of a Python print format function. In this example, we are using the format function inside the print function. It allows us to format values. I suggest you refer to the Python format Function article.

name = 'Tutorial Gateway'
person = 'suresh'
year = 2019
print('{0} is working at {1}'.format(person, name))
print('Copyright {} at {}'.format(name, year))
suresh is working at Tutorial Gateway
Copyright Tutorial Gateway at 2019

Special Characters in Python Print Function

The Python programming language allows you to use special characters by using escape characters. For example, within s1 (‘Hi there, \ “How are You?\”‘), we used \ character to escape double quotes within the statement. Next, we used \ to escape ‘(single quote) in print (‘I Can\’t Do that’).

s = 'Hi there, "How are You?"'
s1 = 'Hi there, \"How are You?\"'
print('I Can\'t Do that')
print('I Don\'t Know you')
Hi there, "How are You?"
Hi there, "How are You?"
I Can't Do that
I Don't Know you

The first statement returns three new lines. And the last statement stamps 5 new lines.

print(3 * "\n")
Python Print 15

This example is the same as the above — however, this time, we used three empty lines in between the two strings Hi and Hello.

print(3 * "\n")
print(3 * "\n")



In this example, we are using the New Line, Horizontal Tab, Vertical Tab, etc., in between a string to show you how they alter the output. For example, the first statement with \n display Hi there, in the first line and How are You? in the next line.

s = 'Hi there, \nHow are You?'
s1 = 'Hi there,\tHow are You?'
s2 = 'Hi there,\vHow are You?'
s3 = 'Hi there,\fHow are You?'
s4 = 'Hi there,\rHow are You?'

print function with New Line, Horizontal Tab, and Vertical Tab output

Hi there, 
How are You?
Hi there,	How are You?
Hi there,How are You?
Hi there,How are You?
Hi there,
How are You?

Conversion Types in Python Print function

The list of conversion types that are available in the Python print function.

  • %c – Returns a Single character.
  • %d – Returns a Decimal Integer
  • %i – for Long integer
  • %u – Returns an unsigned decimal integer
  • %e, %E – Returns the floating-point value in exponential notation.
  • %f – Returns the floating-point value in fixed-point notation.
  • %g – Returns the shorter value of %f and %e
  • %G – Returns the shorter value of %f and %E
  • %c – Returns a Single character
  • %o – Returns an Octal value
  • %r – Generates string with repr()
  • %s – Converts the value to a string using str() function.
  • %x, %X – Returns the Hexadecimal integer.

Let me use all the available conversion types in the Python print function. For this, we declared a few variables with a numeric value, string, decimal value, and a character.

value = 100000
text = 'Tutorial Gateway'
science = 1.20023456341
cr = 'u'
print('%d' %value)
print('%i' %value)
print('%u' %value)
print('%o' %value)
print('%x' %value)
print('%X' %value)
print('%f' %science)
print('%e' %science)
print('%E' %science)
print('%g' %science)
print('%G' %science)
print('%c' %cr)
print('%s is a string representation' %value)
print('%s' %text)
100000 is a string representation
Tutorial Gateway

Python print file example

Here, we are opening a file pythonSample.txt (if it exists). Otherwise, Python creates that text file in the default directory. Next, the it display the statement inside that text file object.

file_name = open('pythonSample.txt', 'w')

print('Welcome to Tutorial Gateway', file = file_name)

Python Print 19