Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Number Game App String length

Jorge Corral
Jorge Corral
3,713 Points

Return vs Print in this example

Why do we use 'return' instead of 'print' in this example?

strlen.py
def just_right(string):
    if len(string) < 5:
        return("Your string is too short")
    elif len(string) > 5:
        return("Your string is too long")
    else:
        return True

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Miguel,

Often when using functions, it's more practical to just return something to be used later, rather than immediately printing it to the screen. If you were to call your function, you could store whatever it returns in a variable, manipulate it however you need to (maybe you need to concatenate it with something else), then print it.

In this particular example, your function will return either a string, or a Boolean value.

Example:

>>> my_string = just_right("tree")

>>> print(my_string)
Your string is too short

>>> print(just_right("treehouse"))
Your string is too long
Jorge Corral
Jorge Corral
3,713 Points

Awesome, thanks Ryan!