Computer >> Computer tutorials >  >> Programming >> Python

How to Sort Words in Alphabetic Order using Python?


Assuming that the a string object contains multiple words separated by one space. The split() method of string class returns a list of words separated by space character. This list object is sorted by invoking sort() method of built-in list class

>>> string='Hello how are you?'
>>> list=string.split()
>>> list
['Hello', 'how', 'are', 'you?']
>>> list.sort()
>>> list
['Hello', 'are', 'how', 'you?']