• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Difference Between print and return in Python

Difference Between print and return in Python

July 25, 2022 Leave a Comment

When working with Python, there are many functions which seem similar but should be used for different cases.

One such example is the use of the print() function and a return statement.

Basically, the difference between print and return in Python is that print prints text to the terminal and return returns data from a function.

These two concepts can be a little tricky to understand when learning Python so hopefully this article will be useful for you to understand the difference between print() and return in Python.

Using print() in Python to Print Text to the Terminal

The Python print() function prints text to the terminal. print() can be useful if you want to see the value of certain variables and can also be used if you want to communicate different messages to the user of the program.

You can use print() in a variety of ways but the most common is to use it and print a variable to the terminal.

Below is a simple example of how you can use print() in Python.

a = "hello"

print(a)

#Output:
hello

You can use print() to print multiple variables to the terminal window.

Below is an example showing you how to use print() to print multiple variables to the terminal in Python.

x = 0
y = 1
z = 2

print(x,y,z)

#Output:
0 1 2

Using return to Return Data from Function in Python

In Python, you use return in functions to return data from a particular function.

The most basic use of return is if you want to return a value from a function. For example, if we have a simple function which adds two numbers together, you can return the sum with return.

Below is an example showing you how to use return in Python. Note here that nothing is printed to the terminal unless you use print().

def sum_two(x,y):
    return x + y

sum_two(10,3)

#Output:

Note here that nothing is printed to the terminal unless you use print().

def sum_two(x,y):
    return x + y

print(sum_two(10,3))

#Output:
13

You can also use return to break out of functions.

Functions terminate when we return a value back, and so to break out of a function in Python, we can use the return statement. In this case, we will return nothing.

Below is an example of how to get out of a function early with a return statement.

def doStuff():
    print("Here before the return")
    return
    print("This won't print")

doStuff()

#Output:
Here before the return

Hopefully this article has been useful for you to learn the difference between print() and return in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Convert Integer to String with Leading Zeros
  • 2.  pandas covariance – Calculate Covariance Matrix Using cov() Function
  • 3.  Using Python to Count Odd Numbers in List
  • 4.  Convert timedelta to Seconds in Python
  • 5.  pandas str replace – Replace Text in Dataframe with Regex Patterns
  • 6.  How to Group By Columns and Find Minimum in pandas DataFrame
  • 7.  Get Random Value from Dictionary in Python
  • 8.  Python Even or Odd – Check if Number is Even or Odd Using % Operator
  • 9.  How to Check if String Contains Lowercase Letters in Python
  • 10.  Python Add Months to Datetime Variable Using relativedelta() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy