Difference between print and return, and when to use it?



n = “Hello”

Your function here!

def string_function(s):
word = s + “world”
return word

print string_function(n)

n = “Hello”

Your function here!

def string_function(s):
word = s + “world”
print word

print string_function(n)

Why does print not work for this?


print -> value displayed then gone from memory.

return -> value handed back to caller

We can print from anywhere, but can only return from a function. Printing has no data bindings. Returned values are bindable.


Unfortunately, there is a character limit so this will be in many parts. First thing to note is that return and print are statements, not functions, but that is just semantics.

I’ll start with a basic explanation. print just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. return is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions.

On a more expansive note, print will not in any way affect a function. It is simply there for the human user’s benefit. It is very useful for understanding how a program works and can be used in debugging to check various values in a program without interrupting the program.

return is the main way that a function returns a value. All functions will return a value, and if there is no return statement (or yield but don’t worry about that yet), it will return None . The value that is returned by a function can then be further used as an argument passed to another function, stored as a variable, or just printed for the benefit of the human user.

Consider these two programs:

def function_that_prints():
    print "I printed"

def function_that_returns():
    return "I returned"

f1 = function_that_prints()
f2 = function_that_returns()
print "Now let us see what the values of f1 and f2 are"
print f1
print f2


Alright, thank you mtf and rajesh19977 for such prompt replies. I sort of get it, now that you said every function requires a return otherwise a None will be returned.

#!/usr/bin/env python

def printAndReturnNothing():
    x = "hello"

def printAndReturn():
    x = "hello"
    return x

def main():
    ret = printAndReturn()
    other = printAndReturnNothing()

    print("ret is: %s" % ret)
    print("other is: %s" % other)

if __name__ == "__main__":

What do you expect to be the output?

ret is : hello
other is: None


Why? Because print takes its arguments/expressions and dumps them to standard output, so in the functions I made up, print will output the value of x , which is hello .

  • printAndReturn will return x to the caller of the method, so: ret = printAndReturn()

ret will have the same value as x , i.e. "hello"

  • printAndReturnNothing doesn’t return anything, so: other = printAndReturnNothing()

other actually becomes None because that is the default return from a python function. Python functions always return something, but if no return is declared, the function will return None .