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

John Michael
1,239 PointsHow to find the longest word of 3 strings
Hi everyone,
I am trying to return the longest word of these three strings. If the strings are the same in length it needs to return both of the words if possible.
def longest (s1, s2, s3):
return max(len(s1, s2, s3))
print (longest ('hello', 'treehouse', 'code')) # should return 'elegant'
print (longest ('code', 'hello', 'treehouse')) # should return 'elegant'
print (longest ('treehouse', 'code', 'hello')) # should return 'elegant'
print (longest ('same', 'same', 'length')) # should return 'elegant'
I believe that you cannot use more then one argument for len()
Thanks for the help.
2 Answers

Jason Anello
Courses Plus Student 94,610 PointsHi John,
You can pass multiple arguments to the max() function.
Probably the closest to what you tried to do would be
def longest (s1, s2, s3):
return max(s1, s2, s3, key=len)
That's saying to return the maximum of the first 3 arguments using the len() function to decide the maximum value. It will only return the first occurrence if there's more than 1 maximal value.
If you want to return all words that have the maximum length then you could get the maximum length and then use a list comprehension to generate a list with all the words that have that maximum length.
def longest (s1, s2, s3):
max_length = len(max(s1, s2, s3, key=len)) # get the maximum word and then the length of that word
return [word for word in [s1, s2, s3] if len(word) == max_length]
In the list comprehension, it's iterating over each word in the list of 3 words. If the length of that word equals the max_length then it will be included in the output list.
If you're not familiar with list comprehensions there's a workshop on them that Kenneth did.

Arthur Kay
Python Web Development Techdegree Student 1,378 Pointsdef longest (s1, s2, s3):
# Transform in a list
word_list = s1 + "," + s2 + "," + s3
word_list = word_list.split(",")
longest = len(max(word_list,key=len))
for word in word_list:
if len(word) == longest:
print(word)
longest('hello', 'treehouse', 'codex')
print("-"*30)
longest('holla', 'hello', 'ks')
I do this, it works lol
John Michael
1,239 PointsJohn Michael
1,239 PointsHi Jason,
Thanks so much for your reply, this is perfect!