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 (2016, retired 2019) Tuples Stringcases

Faisal Rana
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.

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

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

1 Answer

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 (