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

Can't get correct reversed string

I tried both following, just cannot get the correct answer. Please let me know what is wrong:

def stringcases(string): a = string.upper() b = string.lower() c = string.capitalize() d = list(string) d.reverse reve = "".join(d) tpl = (a, b, c, reve) return tpl

def stringcases(string): a = string.upper() b = string.lower() c = string.capitalize() d = string.split() d.reverse reve = " ".join(d) tpl = (a, b, c, reve) return tpl

stringcases.py
def stringcases(string):
    a = string.upper()
    b = string.lower()
    c = string.capitalize()
    d = list(string)
    d.reverse
    reve = "".join(d)
    tpl = (a, b, c, reve)
    return tpl

4 Answers

Noel Barcelona
PLUS
Noel Barcelona
Courses Plus Student 495 Points

if you want to reverse a sting, you can do this:

a = someString

reversedString = a[::-1]

this is in python3 btw.

Thanks a lot. But still can't got the write answer..... Actually, here is the 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

Noel Barcelona
PLUS
Noel Barcelona
Courses Plus Student 495 Points

oh, it's because you're converting the string into a List. It's asking for a string data type, not List data type.

d = list(string) #remove this line of code#
d = string[::-1] #and replace it with this#

you can check out all string operations here: https://docs.python.org/3/library/stdtypes.html#string-methods

reading the documentation can be a bit tedious but really rewarding.

Thanks a lot. In my original code, the list gets joined into a string. but, I have tried [::-1], still cannot get to pass the challenge........ T T

Noel Barcelona
PLUS
Noel Barcelona
Courses Plus Student 495 Points

You can still use your original code just add open and close parenthesis in "d.reverse" so it becomes "d.reverse()" you have to call the function 'reverse'

Thanks a lot!!! Great catch, but still cannot get pass. I mean "reversed" means to make "ab c" to "c ab", right?

Someone just answered my question and then the answer disappeared. However, that answer was the correct one. I should have used .title(), rather than .capitalize(). No idea the system keeps saying "Got the wrong string for Reversed" ..... Got passed now!! Thank you all!!!!

Derek Woods
Derek Woods
6,272 Points

sorry about that i deleted it because by the time i answered there was 3 other answers