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 prints 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 print 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 print Hello world as an output.

# Python Print Examples
 
print("Hello World")
 
print('Hello World!')

Python print function output

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 printing two individual strings, “Learn” and ‘Python Programming’ separated by comma. As we said earlier, it uses space as the default separator so, it prints Learn Python Programming

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

print strings output

Learn Python Programming
Learn Python Programming

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

print()
 
print(' ')

print empty space


 
>>> 

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

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

 
Hello World

Learn Python Programming
 
Hi

Python print variables

When you are calling a variable or printing 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 print statement is calling the variable number value. The second print statement is printing the word number. So, please be careful while calling the variable values or list values, etc.

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

Python print List, Tuple, Set and Dictionary

Using this print function to print the items in ListsTuplesSets, and Dictionary. As you can see from the below, we created two lists, two tuples, three sets, and 1 dictionary. Next, we used the Python print function to print 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']
 
print(numbers_list)
print(fruits_list)
 
numbers_tuple = (15, 25, 35, 45, 55)
fruits_tuple = ('apple', 'cherry', 'kiwi', 'mango')
 
print(numbers_tuple)
print(fruits_tuple)
 
numbers_Set = {1, 2, 3, 4, 5}
fruits_Set = {'apple', 'kiwi', 'banana', 'orange'}
mixed_Set = {'banana', 1, 2, (1, 2, 3)}
 
print(numbers_Set)
print(fruits_Set)
print(mixed_Set)
 
myDict = {'name': 'Kevin', 'age': 25, 'job': 'Developer'}
print(myDict)

print function to print list, tuple, and set items output

[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, the range 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',  'Python Programming', sep = "---")
 
print('Learn',  'Python', 'Programming', sep = " ")
print('Learn',  'Python', 'Programming', sep = " # ")
 
print("Hello",  "World", sep = " AAA ")
print("Hello",  "World", sep = ',,,...,,,')

print Function with separator output

ABC
A B C
Hello*World
Learn---Python Programming
Learn Python Programming
Learn # Python # Programming
Hello AAA World
Hello,,,...,,,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 prints 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 printing the Welcome to, space appended. Next, the fifth print statement prints the Tutorial Gateway after that space, and the statement ends with.  

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

print function with end argument output

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, print(10, 20, 30, sep = ‘ @ ‘, end = ‘ **** ‘) prints 10 20 30 separated by @ symbol and ends with ****. Similarly, print(‘A’, ‘B’, ‘C’, sep = ‘ , ‘, end = ‘  #####  ‘) statements prints 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('-------------')
print(10, 20, 30, sep = ' @ ', end = ' **** ')
print('A', 'B', 'C', sep = '--', end = ' . ')
 
print()
print('A', 'B', 'C', sep = ' , ', end = '  #####  ')
print(10, 20, 30, sep = ' @ ', end = ' !!! ')

print function with end and separator output

10 20 30
A B C
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 Python print function arguments. Here also, we used the print function along with the separator argument and the end argument.

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

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

print function with multiple arguments output

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 print items within a given range — the first statement print numbers from 1 to 9.

for i in range(1, 10):
    print(i)
 
print("--------------")
for i in range(1, 10):
    print(i, end = "   ")
 
print("\n--------------")
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   
--------------
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 print statement, we used two %s in between a string follows by a tuple with variables. It means print function replace those two %s items with those tuple values.

Here, Python print function 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, print(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))

print and format functions output

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 print 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(s)
print(s1)
 
print('I Can\'t Do that')
print('I Don\'t Know you')

print function with special Characters output

Hi there, "How are You?"
Hi there, "How are You?"
I Can't Do that
I Don't Know you

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

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

This print function 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('-----------')
print('Hi')
print(3 * "\n")
print('Hello')

print function with empty lines output





-----------
Hi




Hello
>>> 

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 prints 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(s)
print(s1)
print(s2)
print(s3)
print(s4)

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. 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)

print function conversion types output

100000
100000
100000
303240
186a0
186A0
1.200235
1.200235e+00
1.200235E+00
1.20023
1.20023
u
100000 is a string representation
Tutorial Gateway

Python print file example

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

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

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

file_name.close()
Python Print 19