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

Need help with this section — "Create a function named stringcases"

The instruction are "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."

Here is my code and it works, but it is not being accepted. I guess I'm not understanding the tuple part.

def stringcases(string):
    word=[]
    for index, value in enumerate(string):
        word+=value
    combined_word = ''.join(map(str, word))
    print(combined_word.upper())
    print(combined_word.lower())
    print(combined_word.title())
    print(combined_word[::-1])

stringcases("Hello")

1 Answer

Hi Lindsay,

Tuple is an iterable object like lists and dictionaries. Like lists, tuple items can be selected with their index numbers. However, unlike lists tuples are immutable. Once created, you can not change items in a tuple.

A function usually returns something. In this exercise, you are asked to return a tuple.. so, the last line of the stringcases function will look like:

return (a,b,c)

Actually, this stringcases function does not need to be longer than a single line. You only need to place the content of the print statements in your code in a tuple and then return it.

got it?

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

I hope this helps.