How to Print the output of a Function in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Print the output of a function in Python

To print the output of a function:

  1. Make sure to return a value from the function.
  2. Call the function and store the result in a variable.
  3. Use the print() function to print the result.
main.py
def example(name): return 'site: ' + name result = example('bobbyhadz.com') print(result) # ๐Ÿ‘‰๏ธ 'site: bobbyhadz.com'

print output of function

The code for this article is available on GitHub

Notice that the function uses a return statement to return a value.

This is necessary because all functions that don't explicitly return a value implicitly return None in Python.
main.py
# ๐Ÿ‘‡๏ธ 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.

# The print() function returns None

Note that the print() function returns None, so don't try to store the result of calling print in a variable.

main.py
website = print('bobbyhadz.com') print(website) # ๐Ÿ‘‰๏ธ None

print function returns none

The code for this article is available on GitHub

Instead, store the value in a variable and pass the variable to the print() function.

main.py
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.

main.py
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.

# Common sources of None values

If you are getting a None value when printing the output of the function, the most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

Make sure to return a value in every branch of your function.

main.py
def get_name(a): if len(a) < 5: return a result = get_name('bobbyhadz.com') print(result) # ๐Ÿ‘‰๏ธ None

return value in every branch of your function

The code for this article is available on GitHub

The if statement in the function only runs if the provided argument has a length of less than 5.

In all other cases, the function doesn't return anything and ends up implicitly returning 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.

main.py
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.

# Some built-in functions return None

User-defined functions that don't explicitly use the return statement to return a value return None.

main.py
# ๐Ÿ‘‡๏ธ this function prints a message and returns None def example(name): print('site: ' + name) result = example('bobbyhadz') print(result) # ๐Ÿ‘‰๏ธ None

built in functions returning none

The code for this article is available on GitHub

However, many built-in methods also return None, e.g. sort(), append() and extend().

main.py
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.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev