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] Python Collections - code challenge stringcases

Hey Guys, What am I doing wrong? I tested my code in my terminal and everything seem to come out correctly. What do I have to do to return a tuple? Below is my code.

def stringcases(my_string):
      uppercase = tuple(list(my_string.upper()))
      lowercase = tuple(list(my_string.lower()))
      titlecase = tuple(list(my_string.title()))
      reverse = tuple(list(my_string[::-1]))
      return (uppercase, lowercase, titlecase, reverse)

I ended up doing this to make it clean as well. I'm embarrassed because I at first was trying to make a for loop to iterate through the words in the string and capitalized. I didn't know about title()

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

9 Answers

Hi Curtis,

Which code challenge is this for as I don't remember needing to create a tuple when transforming the case of a string.

It is the string case code challenge for the Tuples section. Below is the question that came with the challenge.

Create a function named stringcases that takes a string and returns a tuple of four versions of the string: uppercased, lowercased, titlecased (where every word's first letter is capitalized), and a reversed version of the string.

Indeed it does, funny how 2 days can make you forget that =)

So what it's asking for is a single tuple containing all 4 types of the string, I've simplified your code a to but it does function the way the challenge expects it to.

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

Thanks Chris!

You're welcome

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

ahhh +1, i was using capitalize() and could not figure out for the life of me why it wouldn't accept it. Everything else was just the same as your 2 line answer above. I had

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

when it should have been:

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

I also had some crazy code to make it reverse, told myself i wouldn't look up the easiest way to reverse til i figured it out myself. I had one of those slap yourself in the forehead moments when i saw the [::-1] on stack overflow. This was my reverse code.

  reversed = []
  x = (len(string)-1)

  while x >= 0:
    reversed.append(string[x])
    x-=1

  reversed_word = "".join(reversed)

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

a,b,c,d= 'kenneth'.upper(), 'love'.lower(), 'developer'.title(), 'best'[-1::-1] def stringcases(): return a,b,c,d i don't know why my answer is showing error when i am tuning on my python ide it is working fine there !! .

Can I ask why if i do

 (st.upper(), st.lower(), st.capitalize(), st[::-1])

It says that it's wrong but the console output is the same?

If you try a string which include space and than capitalize works like below,

my_string = 'hello world'
my_string.title()
=> 'Hello World'
my_string.capitalize()
=> 'Hello world'

It asked for a function this is what is has to be def stringcases(string): return(string.upper(), string.lower(), string.capitalize(), string[::-1])

turaboy holmirzaev
turaboy holmirzaev
19,198 Points

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

So I wrote the correct code, but when I tried in PyCharm the result was not a touple (at least I think). It gave four words, but not separated by commas. Am I wrong in understanding what is a touple?

Hope this can be useful to someone. For this challenge I first created an empty list which I appended with each string format then finally turned the list into a tuple:

def stringcases (argument): my_list = [] for x in argument: my_list.append(argument.upper()) my_list.append(argument.lower()) my_list.append(argument.title()) my_list.append(argument[::-1]) break my_tuple = tuple(my_list) return (my_tuple)

string = "This is a string"

def stirngcases(**string): for str in string: print(str)

print(string[::-1],string.lower(), string.upper(), string.title())