How to Sort Words in Alphabetic Order using Python?


In this article, we will show you how to sort words in alphabetical order in Python. Below are the methods to sort the words in alphabetical order:

  • Using bubble sort

  • Using sorted() function

  • Using sort() function

Using bubble sort

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store an input string.

  • Use the split() function (splits a string into a list. We can define the separator; the default separator is any whitespace) to split the input string into a list of words and create a variable to store it.

  • Use the for loop, to traverse through each word in the words list till the length of the words list using the len() function (The number of items in an object is returned by the len() method. It returns the number of characters in a string when the object is a string)

  • Use the lower() function, to convert all the words into lowercase.

  • Apply the bubble sort to sort the words and print them.

Example

The following program returns the sorted words of the sentence using the bubble sort in Python −

# input string inputString = "the Quick brown fox jumPs over the lazY Dog" print("Input String =",inputString) # splitting the input string into a list of words wordsList = inputString.split(" ") # traversing through each word in the words list till the length of the words list for w in range(len(wordsList)): # converting all the words into lowercase using the lower() function wordsList[w]=wordsList[w].lower() # traversing through each word from the end in the words list # Applying bubble sort to sort the words for n in range(len(wordsList)-1, 0, -1): for i in range(n): if wordsList[i] > wordsList[i + 1]: # swapping data if the element is less than the next element in the array wordsList[i], wordsList[i + 1] = wordsList[i + 1], wordsList[i] # printing the list of sorted Words in alphabetic Order print("Sorted Words Alphabetically =",wordsList)

Output

On executing, the above program will generate the following output −

Input String = the Quick brown fox jumPs over the lazY Dog
Sorted Words Alphabetically = ['brown', 'dog', 'fox', 'jumps', 'lazy', 'over', 'quick', 'the', 'the']


Using sorted() function

The sorted() function returns a sorted list of the iterable object given.

You can choose between ascending and descending order. Numbers are sorted numerically, while strings are arranged alphabetically.

Syntax

sorted(iterable, key=key, reverse=reverse)

Parameters

iterable- It is a sequence.
key - A function that will be executed to determine the order. The default value is None.
reverse - A Boolean expression. True sorts ascending, False sorts descending. The default value is False.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store an input string.

  • Use the sorted() function to sort the words of words list in alphabetical order by passing words list as an argument to it.

  • Print the sorted Words in alphabetic Order and join them with ‘ ’(null string)to convert it into a string using the join() function (It is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to form a string)

Example

The following program returns the sorted words of the sentence using the sorted() function in Python −

# input string inputString = "the Quick brown fox jumPs over the lazY Dog" # splitting the input string into a list of words based on spaces wordsList = inputString.split(" ") # traversing through each word till the length of the words list for w in range(len(wordsList)): # converting all the words into lowercase using the lower() function wordsList[w]=wordsList[w].lower() # sorting the words of words list in alphabetical order sortedWords = sorted(wordsList) # printing the sorted Words in alphabetic Order as a string print(' '.join(sortedWords))

Output

On executing, the above program will generate the following output −

brown dog fox jumps lazy over quick the the

Using sort() function

The sort() method sorts the original list in place. It signifies that the sort() method changes the order of the list's elements.

To put simply, the sorts method sorts the list either in ascending or dissenting order. It is a string it sorts alphabetically.

By default, the sort() method uses the less-than operator (<) to sort the entries of a list i.e., in ascending order. In other words, it prioritizes lesser elements above higher ones.

To sort elements from highest to lowest (descending order), use the reverse=True parameter in the sort() method.

list.sort(reverse=True)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store an input string.

  • Apply sort() function on the words list to sort the words of the words list in alphabetical order.

  • Print the sorted Words in alphabetic Order and join them with ‘ ’(null string) to convert them into a string using the join() function.

Example

The following program returns the sorted words of the sentence using the sort() function in Python −

# input string inputString = "the Quick brown fox jumPs over the lazY Dog" # splitting the input string into a list of words based on spaces wordsList = inputString.split(" ") # traversing through each word till the length of the words list for w in range(len(wordsList)): # converting all the words into lowercase using the lower() function wordsList[w]=wordsList[w].lower() # sorting the words of words list in alphabetical order using sort() wordsList.sort() # printing the sorted Words in alphabetic Order as a string print(' '.join(wordsList))

Output

On executing, the above program will generate the following output −

brown dog fox jumps lazy over quick the the

Conclusion

We learned how to sort the words in a sentence in Python using three different approaches in this tutorial. We also learned how to sort a list using bubbble sort.

Updated on: 09-Sep-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements