How to Use Python Print Function

How to Use Python Print Function

4 mins read310 Views Comment
clickHere
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Mar 29, 2023 21:00 IST

Python print function is used to display the output on the screen. In this article, we will learn how to use python print function using different examples.

2023_01_MicrosoftTeams-image-163.jpg

If you are writing any program or code, and once you are done with it, then you want to display your output on the screen. In python, we have a print function that prints the specified message on the screen or any other standard device. The displayed message can be a string or object (the object, if it is not a string, it will be converted into a string before being displayed on the screen).

Syntax

 
print(object(s), sep = seprator, end = end, file = file, flush = flush)
Copy code

Parameters

  • objects: The value you want to get printed. It may be more than one.
  • sep: 
    • It is an optional value that is used to separate one object from the other.
    • Default Value: ‘ ’ – a space
  • end:
    • It is also an optional value that is used to specify what to print at the end.
    • Default Value: ‘ ’
  • file:
    • An optional value that must be an object with a write method.
    • Default Value: sys.stdout – system standard output
      • sys.stdout is used to print the objects on the screen.
  • flush:
    • It is an optional value that takes Boolean parameters indicating whether the output is flushed (True) or buffered (False)
    • Flush means whether the print call will immediately take effect.
      • Default Value: flush = False

Return Value

The print function has no return value, i.e.
Return Type: None

What is Programming What is Python
What is Data Science What is Machine Learning

Till now, we have gained enough theoretical knowledge of print function in python. Now it’s time to learn how to use python print().
So, let’s start with printing “Hello World!!, We are Learning Python.”

 
#print 'Hello World!!, We are learning Python'
print('Hello World!!, We are learning Python')
Copy code

Output

2023_01_image-171.jpg

How print() works with string literals?

String literals contain textual data in the form of characters enclosed within quotes. These characters are enclosed in double quotation marks (“ ”).

Example 1: Print the value stored in a variable

 
# Define the Variable
company_name = "InfoEdge"
#print
print(company_name)
Copy code

Output

2023_01_image-172.jpg

As we mentioned in syntax, print command can take more than one objects. Multiple objects in print() can be stored by using commas in between different objects.

Example 2: Pass two objects as different arguments in print().

 
# print Hello World!! and I'm working in InfoEdge are passed as two different arguents
print("Hello World!!", "I'm working in InfoEdge")
Copy code
# pass string literals and variable in print()
#string literal: I'm working in
#variable: InfoEdge
# Define the Variable
company_name = "InfoEdge"
#print
print("I'm working in", company_name)
Copy code

Output

2023_01_image-174.jpg

We can get the above output using concatenation. Let’s see the below example.

Example 4: Python Concatenation

 
# Python Concat
# Define the Variable
company_name = "InfoEdge"
#print
print("I'm working in" + company_name)
Copy code

Output

2023_01_image-175.jpg

Must Read: Getting Started with Python String

Must Read: String Formatting in Python

Recursion Function in Python count() Function in Python
len() Function in Python float() Function in Python
range() Function in Python lambda() Function in Python

If you see example 2, where we have passed two objects as a different argument, in print(), then we get the below:

2023_01_image-173.jpg

Here, in the output both the argument (Hello World!! and I’m working in InfoEdge) are not separated. So, how can we separate them.

Let’s see the example below.

How to separate parameter in python print() function?

Example 5: Separate both the arguments of example 2 by a comma.

 
# use sep parameter in print() function
print("Hello World!!", "I'm working in InfoEdge", sep = ' , ')
Copy code

Output

2023_01_image-181.jpg

How to use end parameter in print()?

Example 6:

 
# use end parameter in print()
print("Hello World", end = '!!', sep = ' ')
Copy code

Output

2023_01_image-182.jpg

Example 7:

 
# use end parameter in print()
a = "Vikram"
b = "Shubham"
print(a, end = ' \n\n\n ')
print(b)
Copy code

Output

2023_01_image-183.jpg

Difference Between print() and return() in Python

  • print() is a function, that we call. It shows the human user a string representing what is going on inside the computer.
    • print statement is used when you want to show the value to the user.
  • return() is a keyword, that stops the execution of the current function, sending a value out to where the function was called.
  • return() changes the flow of the program, while print() doesn’t.

In simple terms, printing means showing a value in the console (or to the users), whereas return means giving back a value from a function.

Let’s have an example, where we will use both return() and print.

 
def square(x):
return x*x
def g(y):
return y + 3
def h(y):
return square(y) + 3
print(g(h(2)))
Copy code
About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio