Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping Returning Values

what is the difference between the "len" and "return" because isn't they both returning the value in a function?

I would like to know what is the difference between the "len" and "return" because isn't they both returning the value in a function? In the create a funtion video we used "len" to return a value and in "repeating values" video we used the "return" keyword to return a value. When do I use the len and when do I use the return?

3 Answers

Curtis Vanzandt
Curtis Vanzandt
9,165 Points

len() is a method built into Python. It returns the length of an object. For strings, it returns the amount of characters in that string.

return is a keyword used when writing functions. It's used to return a value, be it a string, integer, boolean value, or whatever.

Yuyang Peng
Yuyang Peng
5,665 Points

len() contains return, and that is why len() returns a value the function len() would look like //just a basic idea len(parameter): return //the length of parameter Curtis already explained very well, I hope this adds some information to it.

return is the keyword used by len(), and all the functions that need to return a value, to actually return a value.

For example:

The len() function uses the return keyword once it has calculated the number of character of the string that was passed in.

def len(string):
     # Calculate the length
     return result