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 (Retired) Tuples Stringcases

Steve Johnson
Steve Johnson
7,034 Points

Wrong output from stringcases?

I cannot fix my code because the output looks correct. It is a tuple with the three cases asked for and one that is reversed: you loves Treehouse

Hi Steve,

Could you please post the code which isn't passing?

Steve Johnson
Steve Johnson
7,034 Points

string = "Treehouse loves you"

def stringcases(string): a = string.upper() b = string.lower() c = string.title()

list = string.split() new_list = [] rev_string = () count = 0 for item in list: new_list.append(list[len(list) - (count + 1)]) count += 1

rev_string = ' '.join(new_list) d = rev_string

return a, b, c, d

print(stringcases(string))

Steve Johnson
Steve Johnson
7,034 Points

This code works:

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

Thanks again!

2 Answers

Slices are read in the format of [a:b:c] where 'a' is the start index, 'b' is the ending index, and 'c' is the steps to take between those. You can leave out all three and just include the semicolons, in which case it's assumed you want to read from start to end in steps of one. If you have a negative step and leave out the start and end, it's assumed that you mean to read backwards, and go from end to start instead (because it wouldn't make sense to read backwards from index 0). You can also include negative indices, which tell your slice to start or stop that many indices backwards from the end.

It's explained in greater detail in Python Collections -> Slices -> Slicing with a step https://teamtreehouse.com/library/python-collections/slices/slicing-with-a-step

Steve Johnson
Steve Johnson
7,034 Points

Thanks so much Evan. Very helpful. I simply forgot how the slice works.

The problem is that you aren't returning the answer as a tuple. Your answer is a little overcomplicated as well; I'd use:

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

Edit: Looking over your code in detail, your original also doesn't reverse the string, it moves the words in the string into reverse order.

Second edit: You're both right, the tuple wasn't the problem.

Steve Johnson
Steve Johnson
7,034 Points

Thanks! My output was a tuple but the last thing you did was new to me: arg[::-1]. It was what I needed. Could you please explain this bit please. (Also, I was trying to reverse the words but not the letters , which was why I had the more complicated code, and the challenge instructions could be taken either way.)

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

It's the comma that makes

return a, b, c, d

a tuple. The parens add clarity but are unnecessary.