Python program to sort words in alphabetical order
In this post, you will learn how to sort words in alphabetic order using the Python programming language.
Python is a high-level, open-source, general-purpose programming language. It enables us to write clear, logical applications for small and large tasks. It has a set of useful libraries, packages, and functions that minimise the use of code in our day-to-day lives. A simple Python programming task is sorting a list of words or items in alphabetical order. You may need to add this feature to sort a list of items in e-commerce application development.
Python sort list alphabetically
Python provides a predefined function sorted() that returns a sorted list of the specified list. It accepts both object types, numbers and strings. Numbers are sorted numerically, and strings are sorted alphabetically. But, we can not provide a list containing both.
sorted(iterable, key, reverse)
Here, the iterable is the required parameter that contains either sort, list, dictionary, or tuple. The key is the optional parameter, which is a function to decide the order parameter. TheĀ reverseĀ is also optional, which accepts a boolean value. False will sort ascending, True will sort descending.
Python program to sort words in alphabetic order
In the given Python program, we have sorted words in ascending order.
vehicles = ("car","bike","truck","motorcycle","train")
x = sorted(vehicles)
print(x)
Output of the above code-
['bike', 'car', 'motorcycle', 'train', 'truck']
Here is the other alphabetic order example in descending order.
vehicles = ("car","bike","truck","motorcycle","train")
x = sorted(vehicles, reverse=True)
print(x)
Output of the above code-
['truck', 'train', 'motorcycle', 'car', 'bike']
Python Sort Words in a String in Alphabetical Order
In the given Python program, we have taken a string input from the user and sort the words in that given string in alphabetical order.
input_str = input("Enter a string: ")
# breakdown the string into a list of words
words = input_str.split()
# sort the list
words.sort()
# display the sorted words
print("The sorted words are:")
for word in words:
print(word)
Output of the above code-
Enter a string: John is smaller than Smith
The sorted words are:
John
Smith
is
smaller
than
Related Articles
Count occurrences of each character in string Python
Python remove punctuation from string
Count vowels in a string Python
Python multiline string
Count consonants in a string Python
Sentiment Analysis in Python
Python String isalpha() Method
Python YouTube Downloader with Pytube
Python projects for beginners
Pandas Converting Strings to datetime
Pandas fillna multiple columns
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module
Text extraction from image
How to Send Text Messages With PHP
Fibonacci Series In Python | Python Program To Print
File Handling in Python
How to convert XML to JSON in Python
Python XML to Dictionary
Serialize Python dictionary to XML