Programming & Tech Tips for Everyone

Print vs Return in Python

Printing and returning are fundamentally different concepts in Python.

  • Printing means displaying a value in the console. To print a value in Python, you call the print() function.
  • Returning is used to return a value from a function and exit the function. To return a value from a function, use the return keyword.

Example

Here is an example of a function that prints a value. If you call this function, you can see a greeting in the console:

def greet(name):
    print("Hello", name)
          
greet("Alice")

Output:

Hello Alice

Here is an example of a function that returns a value. When you call this function with a name input, the function gives you back a value. The value in this case is a greeting with the name. You can store this return value into a variable:

def greet(name):
    return "Hello " + name
          
greeting = greet("Alice")

Notice how running this piece of code does not show anything in the console. This is because the function returns a value. It does not print it.

However, you can print the stored in the greeting variable:

def greet(name):
    return "Hello " + name
          
greeting = greet("Alice")
print(greeting)

Output:

Hello Alice

Conclusion

Printing and returning are completely different things in Python. However, as a beginner, you may sometimes be confused by these two because they are used in similar contexts.

  • Printing means showing a value in the console.
  • Returning means giving back a value from a function.

Thanks for reading.

Happy coding!

Further Reading

Share

Share on twitter
Share on linkedin
Share on facebook
Share on pinterest
Share on email