Last modified: Jan 10, 2023 By Alexander Williams
Find shortest word in string, list, tuple Python
Find the shortest word in a string
string = "Hello my name is Python" # 👉️ String
sp_string = string.split() # 👉️ Split String
result = min(sp_string, key=len) # 👉️ Get shortest word
print(f'The shortest word in string is - {result}.') # 👉️ Print Result
Output:
The shortest word in string is: my.
Find the shortest word in a list
my_list = ['apple', 'banana', 'lemon', 'kiwi'] # 👉️ List
result = min(my_list, key=len) # 👉️ Get shortest word
print(f'The shortest word in list is: {result}.') # 👉️ Print Result
Output:
The shortest word is - kiwi.
Find the shortest word in a tuple
my_tuple = ('apple', 'banana', 'lemon', 'kiwi') # 👉️ Tuple
result = min(my_tuple, key=len) # 👉️ Get shortest word
print(f'The shortest word in tuple is: {result}.') # 👉️ Print Result
Output:
The shortest word is - kiwi.