In this tutorial, we will learn how to arrange the given word in the alphabetical order using python by taking simple examples initially. With the help of this program we can take a word as our input and divides the word into a list of characters and sorts the list alphabetically first and join the each character according to the order and finally it prints the sorted word which will be provided as the output.
Taking a single word as the input for the sorting
The topic which we have choose revolves sorting a word in alphabetical order using python. Sorting data is considered as a fundamental task in python. So, we are just seeing some examples on how the given data can be sorted. If we learn the basic standards of how we can sort the data we can easily solve and sort the data when it is given in enormous. Python offers several built-in functions to make this task easier.
In our example program we can achieve our solution by using different functions like sorted(),list(),join().These are some key functions which we are using to solve our assignment.
You can also refer additional built-in functions of python from the following link:Additional python built-in functions
def alphaword(word): wordlist=list(word) sorted_w=sorted(wordlist) sortedstring=''.join(sorted_w) return sortedstring word=input("Enter the word you want to sort: ") sorted_w=alphabetical(word) print("After sorting according to the alphabetical order: ",sorted_w)
In the above program we use different built-in python functions like sort, list and join. Let us know more about how these functions actually help in sorting and joining the given input word.
- The sorted() function in python sorts the list according to the ascending order. We can also find the length of the values and sort the list or given data according to descending order.
- The list() function in python creates a list object. We can use this list function to create a collection of words and perform the operations on the created list.
- The join() method takes all items which are already sorted using sorted function and joins them into one string providing us our desired output.
Output:
Enter the word you want to sort: geethika After sorting according to the alphabetical order: aeeghikt
By leveraging python built-in functions and methods such as sorted and join we can efficiently sort the words and join them according to our desired output. The above tutorial not only demonstrate the string manipulation but it also involves list handling mechanism which makes python readability ease for the users. Learning the basics of the python would lay a solid foundation to solve complex tasks and various programming challenges not only for beginners but also for the developers too.