Python program to sort all words of a string in alphabetical order

Write a python program to sort all words of a String in Alphabetical order :

In this python programming tutorial, we will learn how to sort all words in alphabetical order. Mainly we are going to use the split method of python string and a for-loop for iterating through the words. If you are not familiar with python string and loops, please go through the tutorials on string and loop first.

To sort all words of a string, first, we need to extract each word and store them somewhere. We will use one list to keep all words of the string. We will sort the words alphabetically in the list and then print out the words one by one.

Following is the algorithm we will use :

Algorithm :

  1. Ask the user to enter a string. Read and store it in a variable.

  2. Split the string into words and put them all in a list.

  3. Sort the words in the list alphabetically.

  4. Using one for-loop, print out the words of the list. Or it will print the words of the string alphabetically.

As you can see above, the main idea of solving this problem is to put all words in a list and sort them alphabetically.

Example Program :

def sortAllWords(given_string):
    words_list = given_string.split()
    words_list.sort()

    print ("Sorted string words are : ")

    for word in words_list:
        print(word," ")


user_string = input("Enter input string : ")
sortAllWords(user_string)

You can also download this program from here.

python sort words in a string

Explanation :

  1. In the example above, we are using one different method for the main process to sort the words in alphabetic order. sortAllWords is the method for sorting the words. This method takes one string as an argument. It sorts the words in the string and prints the result.

  2. For splitting the string into words, we are using the split() method. This method splits the string into words and put all words in a list. words_list is the list we are using here to hold all words.

  3. For sorting all words in the list, we are using the sort() method. This method is used to sort all words alphabetically.

  4. We are using one for-loop to print out the content of the list. As you can see above, we can easily print out the contents of a list using a for loop.

  5. For reading the input from the user, we are using the input() method. This method takes one string argument. It will print this string to the user on console and hold the program waiting for user response.

  6. After the ’enter’ is pressed, it will read the content user has written on the console. In the above program, we are using the user_string variable to hold this content or the string.

  7. We are calling the sortAllWords method with user_string as a parameter to print out the sorted words.

Example :

python sort words in a string

Conclusion :

In this tutorial, we have learned how to sort all words of a string in python. A string is immutable. We can’t change the words or any character in a string directly. For sorting the words in a string, we are creating one list with the words of the string as its elements. The list is mutable. We can modify the list items in python. We are using the ’sort()’ method to sort the contents of the list. That’s it. Try to run the example above and drop one comment below if you have any queries.

Similar tutorials :