Python Program to Sort Words in Alphabetic Order – 2024

Hey guys, in this blog we will see a Python Program to Sort Words in Alphabetic Order.

Code

# Python Program to Sort Words in Alphabetic Order

# To take input from the user
#my_str = input("Enter a string: ")

# breakdown the string into a list of words
words = [word.lower() for word in my_str.split()]

# sort the list
words.sort()

# display the sorted words

print("The sorted words are:")
for word in words:
   print(word)

Output

Enter a string: Hi My Name Is Abhishek Sharma
The sorted words are:
abhishek
hi
is
my
name
sharma
Enter a string: What is the temperature outside ?
The sorted words are:
?
is
outside
temperature
the
what
Enter a string: 2 + 2 is 4
The sorted words are:
+
2
2
4
is
  • Taking a string as input from the user.
  • Lowering the whole string and converting it to a list.
  • Using words.sort() to sort the characters according to their ASCII values.
  • And finally, print them in a sorted manner.

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 514

Leave a Reply

Your email address will not be published. Required fields are marked *