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

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

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

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

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
Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

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.

Script Code
Script Code
6,631 Points

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]

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

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

Script Code
Script Code
6,631 Points

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.

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

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)

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

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.

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

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