Python program to Sort Words According to Their Length

str = input("Insert different strings with coma(,) seperated: ")
first = str.split()
len_first = len(first)
for i in range(len_first - 1):
 for j in range(len_first - 1 -i):
  if len(first[j]) > len(first[j + 1]):
   first[j], first[j + 1] = first[j + 1],first[j]
print(' '.join(first))

 

 

Output:

Insert different strings with coma(,) seperated: python, java, kotlin, c, cpp
c, cpp java, python, kotlin

 

Insert different strings with coma(,) seperated: my, python, string, tutorials, beginners, learn
my, learn python, string, tutorials, beginners,

 

Subscribe For Daily Updates