Tutorial Study Image

Python print()

The print() function helps to print the given message to the screen, or other standard output devices. Before written to the output screen the object will be converted into a string.

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) #where objects can be string,or any object

print() Parameters:

It may take five parameters. Along with the first argument object, the  * indicates it may have more than one. Here sep, end, file, and flush are keyword arguments.

Parameter Description Required / Optional
objects  object to the printed Required
sep  objects are separated by sep. Default value: ' ' optional
end  end is printed at last optional
file must be an object with write(string) method optional
flush  If True, the stream is forcibly flushed. Default value: False optional

print() Return Value

It doesn't return any value; returns None.


Examples of print() method in Python

Example 1: How print() works in Python?

print("Python is fun.")

a = 5
# Two objects are passed
print("a =", a)

b = a
# Three objects are passed
print('a =', a, '= b')


Python is fun.
a = 5
a = 5 = b



1.' ' separator is used. Notice, the space between two objects in the output. 
2.end parameter '\n'' (newline character) is used.
3.file is sys.stdout. The output is printed on the screen.
4.flush is False. The stream is not forcibly flushed.



Example 2: print() with separator and end parameters

a = 5
print("a =", a, sep='00000', end='\\n\\n\\n')
print("a =", a, sep='0', end='')


a =000005

a =05

Example 3: print() with file parameter

sourceFile = open('python.txt', 'w')
print('Pretty cool, huh!', file = sourceFile)


Here first it treies to open or create python.txt.The string object 'Pretty cool, huh!' is printed to python.txt file