Welcome to the Treehouse Community
Looking to learn something new?
Why return a string?
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
From my understanding, the return function is used to return a value to do some additional calculations. I can see how this applies for number values but what calculation (other than string concatenation) would you do with a string as in this CC?
Bonus: How do I share my code (with those gray boxes) especially for a CC when I post here on Community? For this one I had to re-enter code into Workspaces, take a snapshot, and then copy the link to it.
Values are not returned just to do calculations, that's just one example of what you can do. The
return statement is essentially just a reverse example of passing an argument into a function. The function is giving a value back to the code that called it and once returned the value can be used for anything that value normally could be.
Let's say you had a function that was designed to present the user with a list of options and then asked the user to choose one of them. That function would not be very useful if i did not tell the code that called it what the user actually ended up choosing, which it can easily do by returning the choice.
Also consider built-in methods in Python like
upper (uppercase a string) and
lower (lowercase a string) those are methods that take a string and then returns a string that has had specific formatting applied to it. They would not serve much use if they did not ultimately return the string they created from the string you called them on.
You can return literally any value in Python, not just strings and numbers. And as you get farther into the courses you'll start to get a better understand of why that is a useful. There are many functions where the function serves no real purpose if it does not actually return the value it worked on after it has finished.
As for your question on code formatting, there is a link below the box where you write your comment labeled "Markdown Cheatsheet" which when clicked opens up a window with various formatting tips. For code formatting you simply need to write three backticks ``` then the name of the language you are embedding followed by a new line. Then paste in your code, then on a new line below your code enter three more backticks.