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

Dezhi Zhu
Dezhi Zhu
2,662 Points

what's wrong?

Create a function named stringcases that takes a single string but returns a tuple of different string formats. The formats should be: All uppercase All lowercase Titlecased (first letter of each word is capitalized) Reversed There are str methods for all but the last one.

stringcases.py
def stringcases(args):

    a = str(args).upper()
    b = str(args).lower()
    c = str(args[0:1]).upper() + str(args[1:]).lower()
    d = args[::-1]

    return(a,b,c,d)

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

It's the third one that's wrong. Notice what the challenge's definition of Titlecased is:

"(first letter of each word is capitalized)"

Your version will work correctly if the string only has one word, but will fail if the string contains two or more words. There is a string method which does exactly what you need:

c = args.title()

As an aside, there's a simpler way to implement what you did (first letter of string upper case, everything else lower case):

c = args.capitalize()

This one would still fail the challenge though.

Also, notice that there's no need to convert your args variable to a string, because the function already expects a string to be passed in. Using str(args) won't break the program, but it's unnecessary in this case.

Dezhi Zhu
Dezhi Zhu
2,662 Points

Hi Stuart Thank you for helping. It is really useful. But I am wondering as a beginner, how could I to know all of these functions and how to know the purposes of them? Is there any way I can access these functions?

Best Regards Dezhi

Stuart Wright
Stuart Wright
41,120 Points

There's no need to memorize them. Try Googling something like "python string first letter of each word uppercase". The first result I get is:

https://stackoverflow.com/questions/1549641/how-to-capitalize-the-first-letter-of-each-word-in-a-string-python

And the top answer has exactly what you're looking for. You'll be amazed how often this is the case.

Alternatively, you can find a list of all string methods in the documentation:

https://docs.python.org/3.6/library/stdtypes.html#string-methods