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

competent- fellow
competent- fellow
5,846 Points

Blocked on Tuples: Stringcases

I am blocked on the tuples: Strincases code challenge. I use the following (perhaps overly complex) code to get all the cases:

def stringcases(string): upper = string.upper() lower = string.lower() titlecased = string.capitalize() str_reversed = reverse(string) return (upper, lower, titlecased, str_reversed)

def reverse(string): str_list = list(string) str_reversed = [] for letter in str_list: str_reversed.insert(0, letter) string = ''.join(str_reversed) return stringdef stringcases(string): upper = string.upper() lower = string.lower() titlecased = string.capitalize() str_reversed = reverse(string) return (upper, lower, titlecased, str_reversed)

"tomatoes" returns ('TOMATOES', 'tomatoes', 'Tomatoes', 'seotamot'), which I think is correct. But I get a "Bummer: Got the strong string for" [blank] message.

Maybe a bug?

Thanks in advance for your help!

stringcases.py
def stringcases(string):
    upper = string.upper()
    lower = string.lower()
    titlecased = string.capitalize()
    str_reversed = reverse(string)
    return (upper, lower, titlecased, str_reversed)

def reverse(string):
    str_list = list(string)
    str_reversed = []
    for letter in str_list:
        str_reversed.insert(0, letter)
    string = ''.join(str_reversed)
    return string

1 Answer

def stringcases(string):
    first = string.upper()
    second = string.lower()
    third = string.title()
    forth = string[::-1]
    return first, second, third, forth 

or

    def stringcases(string):
    return (string.upper(), string.lower(), string.title(), string[::-1])

both are correct but one is better(the first one), because it much more readable(imagine 3000 line of code and 1 error #were you forgot the ","). for the forth string we used slice to reverse . [::]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Pro tip: If you encase your code blocks in ```python and ``` (leaving a blank line outside the makers before and after, you will get nicely colorized code. The ` is typically found left of the 1 key

def stringcases(string): return (string.upper(), string.lower(), string.title(), string[::-1])

becomes

def stringcases(string):
    return (string.upper(), string.lower(), string.title(), string[::-1])