Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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