How to find the second largest word in a string with Python

1 Answer

0 votes
def second_largest_word_in_string(s):
    words = s.split()
    
    # Sort the words by length in descending order
    words.sort(key=len, reverse=True)
    
    if len(words) > 1:
        return words[1]
    else:
        return None


string = "c cpp scala c# python java";

print(second_largest_word_in_string(string))  



'''
run:

scala

'''

 



answered Oct 3, 2024 by avibootz

Related questions

1 answer 95 views
1 answer 90 views
1 answer 95 views
1 answer 102 views
1 answer 93 views
1 answer 118 views
...