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 (Retired) Tuples Stringcases

problems with stringcases

i dont know how to reverse a string. and i think this code is missing something because it keeps saying "try again"

stringcases.py
# Handy functions:
# .upper() - uppercases a string
# .lower() - lowercases a string
# .title() - titlecases a string
# There is no function to reverse a string.
# Maybe you can do it with a slice?
def stringcases():
    string_case = "hi how are you"
    x = string_case.upper()
    y = string_case.lower()
    z = string_case.title()
    u = string_case[:-1]
    return x, y, z, u

8 Answers

Did you try the function I posted yesterday? I just copied and pasted it into the code challenge below the comments and it passed. Here it is again:

def stringcases(string):
    x = string .upper()
    y = string .lower()
    z = string .title()
    u = string [::-1]
    return x, y, z, u

Hey Fhatuwani,

You were close, you don't need to declare a string_case variable, because the function is going to be passed a string. So, you need to add a parameter inside the parentheses after the function name. That parameter then becomes the object that the .upper(), .lower(), and .title() methods are performed on - as well as the slice ([::-1]).

You're function should look like this:

def stringcases(string):
    x = string .upper()
    y = string .lower()
    z = string .title()
    u = string [::-1]
    return x, y, z, u

I hope that helps! Let me know if you have any more questions.

Try u=string_case[::-1] remember that slice operations have [i:j:k] whereas k is an optional stride (a.k.a step). If left out it is uses its default value 1. In your script you defined i and j and left out k.

Try to play with the stride value. [1,2,3,4,5][::-2] This would return [5,3,1]

[1,2,3,4,5][4::-2] This would also return [5,3,1]

hi, i tried running it with [::-1], but it keeps telling me to "try again." i think there is a problem with my def or return

What is your exact assignment?

Because as it is right now it is valid code. This is what your function returns. a = stringcases()

print(a) ('HI HOW ARE YOU', 'hi how are you', 'Hi How Are You', 'uoy era woh ih')

Script Code

The challenge is linked to at the top right corner in case you need to review.

Thanks Jason!

i also dont know this is what i did

def stringcases(): string_case = "hi how are you" x = string_case.upper() y = string_case.lower() z = string_case.title() u = string_case[::-1] return (x, y, z, u)

this is frustrating, coz my script is right but i cant get through, it keeps on saying "try again" and ive tried everything i know to try. its impossible

Did you take a look at Chris Jones answer?

There were 2 things that you needed to fix.

yhooo thanks guys, didnt know that a mistake so simple could hold me up for so long

No problem! We've all been there...sometimes we just get stuck in a certain way of thinking about it:).