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 Collections (2016, retired 2019) Tuples Stringcases

When to wrap str() around varibables?

Hi, I was wondering when do we need to use str()?
I got this exercise to work using str() & without using str(). I can only think of its use when a user insert an input and str should be wrapped around the variable in case user type in a number.

Are there other occasions when str() should be used?

stringcases.py
def stringcase(x)
return(str(x.upper(), str(x.lower()), str(x.title()), x[::-1]))

1 Answer

Steven Parker
Steven Parker
229,644 Points

The use of "str" is primarily for when you have something that is not a string and you want a string that represents it. This is not the case for user input, because the return value from the "input" function is always a string (but you might convert it into a number).

As you noticed, since this exercise only involves strings and functions that return strings, "str" is not needed.