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

Code Challenge: Stringcases

I'm working on this code challenge: "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"

So far I've come up with the following solution which works for strings with an even number of words, but not for strings with an uneven number of words. In the last case it cuts out four letters of the middle word in the string. My guess is this has something to do with the string.split() calls or the different slices because when I comment out the 'titlecase' line this problem does not occur. Can someone help me solve this?

def stringcases(string):
    uppercase = string.upper()
    lowercase = string.lower()
    titlecase = " ".join(w[:1].upper() + w[1:] for w in string.split(" "))
    reverse = " ".join(string.split()[::-1])   
    return (uppercase, lowercase, titlecase, reverse)

2 Answers

def stringcases(string):
    a = string.upper()
    b = string.lower()
    c = string.title()
    d = string[::-1]
    return (a,b,c,d)

Here's what I got. I was having some trouble earlier where the return line looked it was indented 4 spaces but after I copied and pasted it in to ATOM it was actually only indented 2 spaces. Could be a bud when copying and pasting from an IDE.

According to the checker the only one not accepted is reverse. There you are reversing the order of the words. You need to reverse the letters in the words as well.

That the letters in the words need to be reversed as well is not specified at all. Would be nice if these challenges gave us useful feedback instead of just "Bummer. Try again".

But my question was why some letters are dropped in strings with an uneven number of words, but not in strings with an even number of words.