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 trialsatish kumar
1,083 PointsI am aware that my string_4 isnt gonna produce the string that i want so how do i make the word capital at the end
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):
my_tuple=()
my_list=[]
string_1=""
string_2=""
string_3=""
string_4=""
string_1= string.lower()
string_2= string.upper()
string_3= string.capitalize()
string_4= string[-1].capitalize()
my_list.append(string_1)
my_list.append(string_2)
my_list.append(string_3)
my_list.append(string_4)
my_tuple=tuple(my_list)
print(my_tuple)
stringcases("james")
1 Answer
Stuart Wright
41,120 PointsA few notes about your code:
- Not sure if this is just the way the formatting has worked in your post, but you need to unindent the function header so that it is left aligned.
- There's no harm in defining my_tuple, but it doesn't achieve anything either, since you end up overwriting it in the second last line of your function.
- Likewise there is no point in defining string_1 - string_4 as blank strings, as you're about to overwrite these anyway.
- You have .lower() and .upper() the wrong way around from what the challenge wants. It wants upper to come first, then lower.
- For string 3, the method you are looking for is .title(), not .capitalize(). The difference is that .title() capitalizes the first letter of every word in your string, but .capitalize() only capitalizes the first letter of the string.
- For string 4, you don't have to capitalize anything, you just need to reverse it. The best way to do this is using string slicing notation: string[::-1].
- The challenge has asked you to return the final list, not print it.
- I'm not sure whether or not this will cause you to fail the challenge, but you are not asked to call the function (which is what you have done when you pass "james" to the function on your final line).