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

Faisal Rana
seal-mask
.a{fill-rule:evenodd;}techdegree
Faisal Rana
Python Web Development Techdegree Student 2,008 Points

What's wrong with my code ? When I run it in IDLE it shows " AttributeError: 'tuple' object has no attribute 'upper' "

Here is the Challenge: 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.

stringcases.py
def stringcases(*string):
    return string.upper()
    return string.lower()
    return string.title()
    return string.reverse()

stringcases("syntax", "functions", "methods", "tuples", "dict")

1 Answer

diogorferreira
diogorferreira
19,363 Points

Hey, in this challenge they would pass you a single string such as "functions" - as it's not multiple arguments (a list), there is no need to unpack the string variable. It would result in an error anyways as you cannot call .lower() or any of the other functions on a tuple or list.

  • Regarding the last return statement, it would also raise an Error as .reverse() does not exist, there are many ways to reverse a string but by far the easiest one, in my opinion, is to just use string[::-1] - it returns all characters of the string backwards
  • The last thing would be returning a tuple of the answers - just put them in a single return statement and separate them with commas (you could add brackets if it makes it easier to understand)
def stringcases(string):
    return (
        string.upper(), 
        string.lower(), 
        string.title(), 
        string[::-1]
        )