Question

what is different between return and print statement.?

Posted February 14, 2020 91 views
Python

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.

1 answer

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"
print(my_str)

You would expect the output in the console to be:

Output
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()
print(my_str)

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

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

Submit an Answer