what is different between return and print statement.?

Posted February 14, 2020 519 views

python programming

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

Hi there @jamshed2019,

In Python, the print function outputs the given contents to the console window.

For example, if you were to run:

my_str = "Hello world"

You would expect the output in the console to be:

Hello world

This is different from the return keyword, which is used within functions to terminate the function and return a value to whatever called the function initially.

For example, if you run:

def get_hello_world():
    part_one = "Hello"
    part_two = "world"
    return part_one + " " + part_two

my_str = get_hello_world()

You would expect the output in the console to then be:

Hello world

What happened here is that the function get_hello_world() was defined, which the variable my_str then called. The value "Hello world" was returned by the function and assigned to the variable. The contents of the variable were then printed as we showed in the previous example.

I highly recommend the tutorial series that we have published on How to Code in Python3 as it covers concepts like this and much more!

Hope that helps in understanding the difference between the two!
- Matt.

Print() is a function that is used to print a value, string or characters on the console. If you want to show value then use the print() function.

Return statement returns control to the calling function and terminates the execution of a function. In other words, when we reached to return statement, Python will stop the execution of the current function, send value to where the function was called.
For example:

def add(p,q):

n1=input(“Enter value of a :”)
n2=input(“Enter value of b :”)
print(“sum of 2 nos:”,result)