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

Tim Betz
Tim Betz
15,146 Points

Python Tuples challenge

So I have been testing this outside the code challenge and it seems to work. Why isn't it working here? The tuple should have the string in uppercase, lowercase, capitalized and reversed.

stringcases.py
def stringcases(string):
    # return (string.upper(), string.lower(), string.capitalize(), string[::-1])
    upper = string.upper()
    lower = string.lower()
    capitalize = string.capitalize()
    reverse = string[::-1]

    result = (upper, lower, capitalize, reverse)

    return result

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

The correct method to capitalize the first letter of a string is .title()

def stringcases(string):
    upper = string.upper()
    lower = string.lower()
    capitalize = string.title()
    reverse = string[::-1]

    result = (upper, lower, capitalize, reverse)

    return result

Edit to add - actually .title() seems to capitalize first letter of every word in string, and .capitalize() capitalizes first letter of string only. Given the instructions yours should arguably be correct, but my code passes the challenge.

Yes, their request for that portion is oddly misleading. It suggests Titlecased, but then states "the first letter" without adding the "in each word" requirement. I assume they test it with at least one sentence and it fails because of this. There are several Python challenges here that are like this, and I've even come across one that I wasn't able to complete at all, even though my code works flawlessly outside the challenge environment (from what I can tell). They need to rework the courses for Python here.

Stuart Wright
Stuart Wright
41,120 Points

Yes I agree that the wording of this challenge isn't ideal.

Curious as to which challenge you weren't able to complete at all?

Stuart Wright - The Word Count one from the previous set of lessons before Tuples wouldn't complete for me. I solved it 3 different ways on my local system and in online playgrounds, but for some reason my code wouldn't pass. I eventually just posted a request in Community and skipped to the next challenge, which I solved without issue.

I guess I'm just too creative or overlooked an edge-case somewhere in my code, but I tested it with entire pages of text and against the example string with the expected results.. The only thing I could think of was that my dictionary was not in the same order as the shown example result, which makes no sense to me, since dictionaries in Python are unordered anyway. My thought is that maybe the challenge answer was taken too literally and my results just weren't "perfectly" in line with it.

Stuart Wright - Interestingly enough, I just went back and completed the challenge just fine. Want to know the reason I was failing? I was stripping punctuation from the sentence because I thought: "Hey, punctuation shouldn't count as a part of a word, so let's sanitize the string after taking it lower()." I used exactly the same code as before, minus one little detail. Sometimes these challenges are just set up weird.

The problem seems to be in the way the challenge was worded and/or written. If you change capitalize() to title() it will complete the challenge. This seems to indicate that they, in fact did, pass a string with more than one word when testing your method of solving the challenge and they quite literally meant "Titlecased" like the challenge stated. If this is the case, then they should remove the parenthetical comment about "first letter is capitalized" or increase the specificity of the request to include this information.