Last updated: Apr 9, 2024
Reading timeยท3 min

To print the output of a function:
print() function to print the result.def example(name): return 'site: ' + name result = example('bobbyhadz.com') print(result) # ๐๏ธ 'site: bobbyhadz.com'

Notice that the function uses a return statement to return a value.
None in Python.# ๐๏ธ This function prints a message and returns None def example(name): print('site: ' + name) result = example('bobbyhadz') print(result) # ๐๏ธ None
The function in the example doesn't use the return statement, so it implicitly
returns None.
The print function takes one or more
objects and prints them to sys.stdout.
Note that the print() function returns None, so don't try to store the
result of calling print in a variable.
website = print('bobbyhadz.com') print(website) # ๐๏ธ None

Instead, store the value in a variable and pass the variable to the print()
function.
website = 'bobbyhadz.com' print(website) # ๐๏ธ bobbyhadz.com
To print the output of a function, you have to call the function, store the result in a variable and print the result.
def do_math(a, b): return a + b result = do_math(12, 13) print(result) # ๐๏ธ 25
We called the function with parentheses, providing a value for each of its
required parameters and called the print() function with the output.
If you are getting a None value when printing the output of the function, the
most common sources of None values are:
None implicitly).None.Make sure to return a value in every branch of your function.
def get_name(a): if len(a) < 5: return a result = get_name('bobbyhadz.com') print(result) # ๐๏ธ None

The if statement in the function only runs if the provided argument has a
length of less than 5.
None.To get around this, we can return a default value if the condition is not met,
e.g. an empty string, 0, or any other value that suits your use case.
def get_name(a): if len(a) < 5: return a return '' # ๐๏ธ return an empty string if the condition is not met result = get_name('bobbyhadz.com') print(result) # ๐๏ธ ''
Now the function is guaranteed to return a value regardless of whether the condition is met.
User-defined functions that don't explicitly use the return statement to
return a value return None.
# ๐๏ธ this function prints a message and returns None def example(name): print('site: ' + name) result = example('bobbyhadz') print(result) # ๐๏ธ None

However, many built-in methods also return None, e.g. sort(), append() and
extend().
a_list = ['bobby', 'hadz', 'com'] result = a_list.sort() print(result) # ๐๏ธ None
The sort() method returns None, so the variable stores a None value.
There is a convention in Python for methods that mutate an object in place
(directly) to return None.
In this case, we can't print the output of the function when we call it.
Instead, we should print the object after it has been mutated.
a_list = ['bobby', 'hadz', 'com'] a_list.sort() print(a_list) # ๐๏ธ ['bobby', 'com', 'hadz']
We first called the sort() method. The method sorted the list in place and
returned None.
Then, we printed the sorted list.
I've also written an article on how to assign Print() output to a variable.