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 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 :
- Ask the user to enter a string. Read and store it in a variable.
- Split the string into words and put them all in a list.
- Sort the words in the list alphabetically.
- 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.
- 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.
- 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.
- For sorting all words in the list, we are using the sort() method. This method is used to sort all words alphabetically.
- 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.
- 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.
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.
- We are calling the sortAllWords method with user_string as a parameter to print out the sorted words.
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 :
- How to use Hashlib to encrypt a string in python
- Python program to find all numbers in a string
- Python 3 program to check if a string is pangram or not
- Python 3 program to count the total number of characters in a string
- Find total number of lowercase characters in a string using Python 3
- Python program to remove characters from odd or even index of a string