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

Andrei Oprescu
Andrei Oprescu
9,547 Points

How am I supposed to return this tuple? Can anyone give me an example? I am confused.

In a challenge that I am supposed to do, I have gotten this question:

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.

If anyone could give me some advice on how to start this challenge or how the result should look like I would appreciate it!

Thanks!

Andrei

stringcases.py
def stringcases(word):
    word

4 Answers

Steven Parker
Steven Parker
231,269 Points

This function essentially does 4 different things and then returns them all. Returning a tuple is just a matter of listing each thing separated by a comma. For example:

def stringcases(word):
    format1 = "replace this"
    format2 = "replace this"
    format3 = "replace this"
    format4 = "replace this"
    return format1, format2, format3, format4

Of course, you'll need to change each "replace this" with the code that creates what the instructions ask for.

Andrei Oprescu
Andrei Oprescu
9,547 Points

You made the challenge clear to me now, but can you explain why might my code be wrong?

My code:

def stringcases(word):

word1 = str(upper(word))

word2 = str(lower(word))

word3 = str(lower(word2[0]))

word4 = str(word[::-1])

return word1, word2, word3, word4

it gives me:

Bummer: couldn't find 'stringcases'

Thanks!

Andrei

Steven Parker
Steven Parker
231,269 Points

Here are the issues I notice:

  • unlike functions and arguments, methods are applied with dot notation (such as: "word.upper()")
  • these things are strings already, so they don't need to be enclosed with "str()"
  • it looks like you may have misunderstood the instructions for the third format
Andrei Oprescu
Andrei Oprescu
9,547 Points

Hi again,

I have understood what I have done wrong and I have changed what I did wrong.

Now my code is:

def stringcases(word):

word1 = word.upper()

word2 = word.lower()

word3 = word2.upper(0)

word4 = word[::-1]

return word1, word2, word3, word4

But the problems I have now are:

For word3, I forgot how to only uppercase the first letter. What have I misunderstood at word4? How should it look like?

Can you please answer the questions above?

Thanks!

Andrei

Steven Parker
Steven Parker
231,269 Points

As mentioned in the instructions, the first 3 items can be done using string methods, so they all will look alike except for the method name. You can look up string methods, or you might even guess the name of the one you need based on what it does.

Andrei Oprescu
Andrei Oprescu
9,547 Points

Oh, thanks!

Now I understand what string method I had to use and in the future, I will adopt the same strategy to my code!

Andrei