w3resource

Python: Find the longest string of a list of strings

Python Programming Puzzles: Exercise-15 with Solution

Longest String in List

Write a Python program to find the longest string in a given list of strings.

Input:
['cat', 'car', 'fear', 'center']
Output:
center

Input:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Output:
shatter

Visual Presentation:

Python: Find the longest string of a list of strings.

Sample Solution-1:

Python Code:

# Define a function named 'test' that takes a list of strings 'words' as input
def test(words):
    # Use the max function to find the string with the maximum length in 'words' based on the key=len (length of each string)
    return max(words, key=len)

# Create a list of strings 'strs' with specific elements
strs = ['cat', 'car', 'fear', 'center']

# Print the original list of strings
print("Original strings:")
print(strs)

# Print a message indicating the operation to be performed on the list
print("Longest string of the said list of strings:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

# Create a different list of strings 'strs' with specific elements
strs = ['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']

# Print the original list of strings
print("\nOriginal strings:")
print(strs)

# Print a message indicating the operation to be performed on the list
print("Longest string of the said list of strings:")

# Print the result of the test function applied to the modified 'strs' list
print(test(strs))

Sample Output:

Original strings:
['cat', 'car', 'fear', 'center']
Longest string of the said list of strings:
center

Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Longest string of the said list of strings:
shatter

Flowchart:

Flowchart: Python - Find the longest string of a list of strings.

Sample Solution-2:

Python Code:

# License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a list of strings 'words' as input
def test(words):
    # Define the lowercase and uppercase alphabet characters
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    alphabet = alphabet + alphabet.upper()
    
    # Initialize an empty dictionary 'alphabet_dict' to store True for each alphabet character
    alphabet_dict = {}
    
    # Populate the 'alphabet_dict' with True for each alphabet character
    for k in alphabet:
        alphabet_dict[k] = True
    
    # Create a set 'alphabet_set' containing all alphabet characters
    alphabet_set = set(alphabet)
    
    # Initialize 'max_word' with the first word in the list
    max_word = words[0]
    
    # Iterate through each element 'el' in the list 'words'
    for el in words:
        # Check if 'el' contains only alphabet characters and is a subset of 'alphabet_set'
        # Also, check if it has the same set of characters as its intersection with 'alphabet_dict.keys()'
        if not ((set(el) <= alphabet_set) and (set(el) == set(el).intersection(alphabet_dict.keys()))):
            continue
        
        # Check if the length of 'el' is greater than or equal to the length of 'max_word'
        if len(el) >= len(max_word):
            # Update 'max_word' if the length condition is met
            max_word = el
    
    # Return the longest valid word
    return max_word

# Create a list of strings 'strs' with specific elements
strs = ['cat', 'car', 'fear', 'center']

# Print the original list of strings
print("Original strings:")
print(strs)

# Print a message indicating the operation to be performed on the list
print("Longest string of the said list of strings:")

# Print the result of the test function applied to the 'strs' list
print(test(strs))

# Create a different list of strings 'strs' with specific elements
strs = ['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']

# Print the original list of strings
print("\nOriginal strings:")
print(strs)

# Print a message indicating the operation to be performed on the list
print("Longest string of the said list of strings:")

# Print the result of the test function applied to the modified 'strs' list
print(test(strs))

Sample Output:

Original strings:
['cat', 'car', 'fear', 'center']
Longest string of the said list of strings:
center

Original strings:
['cat', 'dog', 'shatter', 'donut', 'at', 'todo', '']
Longest string of the said list of strings:
shatter

Flowchart:

Flowchart: Python - Find the longest string of a list of strings.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Find the lengths of a list of non-empty strings.
Next: Find the strings in a list containing a given substring.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/puzzles/python-programming-puzzles-15.php