Python Print Function

basic of python,

Sourabh Somani Sourabh Somani Follow Apr 10, 2018 · 2 mins read
Share this

Introduction

In this tutorial we will learn about python print function

Syntax

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Parameters of the print() function

As you can see into the syntax, there are five parameters which we can pass to the print() function

  • *Object

    Whatever object which you want to print on the screen or to the file. Here you can see the print function prefixed with the asterisk (*). Which says that you can pass more than one object.

  • sep

    sep means separator which default value you can see one space.

  • end

    After every print statement what you want to print that you will define in end parameter. The default value of the print statements is Newline (\n)

  • file

    By using file parameter you can write your output into the file stream. By default its value is sys.stdout, It means output will be printed onto system’s standard output stream, means will be printed on the console.

  • flush

    It takes either True or False. When the value will be true stream will forcefully flushed. By default its value is False.

Example: Simple Print Function

# Passing string object  
print("Hello Python Babu") # It will print simple Hello Python Babu

Example: multiple objects passing in print() without the separator

# Passing multiple object with space 
# Because "sep" parameters default values is space (" ")
print(2,3,4) # It will print 2 3 4

Example: Using “sep” parameter

# Changing the value of the "sep" parameter
print("Python","Babu",sep="-")

Example: Understanding “end” parameter

'''-------------Example 1-------------'''

# Print without any object
print("Hello")
print() # There is no object but default value of the end parameter is '\n'
print("Welcome to Python Babu")

'''-------------Example 2-------------'''

print("Hello",end='! ') # It will print Hello! 
print("Python",end='-') # It will print Python-
print("Babu",end='.') #It will print Babu.
# So the Complete output will be Hello! Python-Babu

Example: Writing file using print()

# Writing file using print function
myfile = open('PythoBabuFile.txt', 'w')
print('Welcome to https://www.pythonbabu.com', file = myfile)
myfile.close()

What python print() function returns?

Python print() function returns None

Example: returning value from the print function

result=print("Hello") #return value will be save into result
print(result) # It will print none because print returs None
Join Newsletter
Get the latest news right in your inbox. We never spam!
Sourabh Somani
Written by Sourabh Somani Follow
Sourabh Somani is a Full-Stack Engineer with several years of hands-on experience. He has been awarded 4-times Microsoft MVP award consecutively and C# Corner MVP Award 6-times. Most of the time you will find him helping people on C# Corner and StackOverflow on JavaScript, .NET and Python. He is a frequent contributor to open source software development via forums, articles, and blogs. He regularly speaks at user groups and developer conferences. His core technology interests are JavaScript, Python and .NET.