Python Program to Sort Words in Alphabetical Order
# Python Program to Sort Words in Alphabetical Order
# Sample text message for sorting
input_text = "This is a sample string which we will use for sorting"
# You can also take input string from the user
#input_text = input("Enter a string: ")
# STEP 1: convert string to list
input_text_list = input_text.split()
# STEP 2: convert list words to lowercase
input_text_list_lower = list(map(str.lower,input_text_list))
# alternatively you can merge STEP 1 and STEP 2 using single statement
# input_text_list_lower = [element.lower() for element in input_text.split()]
# STEP 3: Sort the list
input_text_list_lower.sort()
print("The sorted words list is:", input_text_list_lower)
seperator = " "
input_text_sorted = seperator.join(input_text_list_lower)
print("Sorted Statement: ", input_text_sorted)
Output:
The sorted words list is: ['a', 'for', 'is', 'sample', 'sorting', 'string', 'this', 'use', 'we', 'which', 'will']
Sorted Statement: a for is sample sorting string this use we which will