Python Python Collections Tuples Stringcases

Cameron Wood
Cameron Wood
2,566 Points

This works perfectly fine in my IDLE but not here...someone please help!

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 There are str methods for all but the last one.

YES. I understand I can use .title and .capitalize but that's not my question. My question is why isn't this working on the website but seems to be fine in IDLE.
def stringcases(s):
    formats = []
    first = ''
    second = ''

    for i in s:
        first += i.upper()
        second += i.lower()

    second = second[0].upper() +second[1:]
    s = s[::-1]
    new = tuple(formats)
    return new

1 Answer

24,111 Points

The issue is that your function does not really title-case the string. As the instructions state title-case is where the first letter of every word is capitalized. Your code capitalizes the first letter of the string, which will work fine if there is only one word passed in, but won't work if a sentence is passed in.

For example if I pass in the string "hello, world!" this is the result that should be returned by the function:

('HELLO, WORLD!', 'hello, world!', 'Hello, World!', '!dlrow ,olleh')

And here is the result your function returns:

('HELLO, WORLD!', 'hello, world!', 'Hello, world!', '!dlrow ,olleh')

Notice the lack of capitalization of the word "world" in the third string. That is the issue with your code.

Properly title-casing a string manually is a somewhat cumbersome process, which is why the title method was added to python in the first place. If you make use of the built-in formatting function then you can solve the challenge with a single line of code:

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