Example HTML page

A Python Program to Sort Words in Alphabetical Order

In this post i am describing how to sort words in alphabetical order. The following is a python program to sort words in Alphabetical order.

S = "a b c d e f g H I J K L m N O p q R s t U V w x y Z"
words = S.split( )
words.sort( )
print("The sorted words are:")
for word in words:
   print(word)

Output

The sorted words are:

H

I

J

K

L

N

O

R

U

V

Z

a

b

c

d

e

f

g

m

p

q

s

t

w

x

y

Explanation

Firstly I defined a string S.  Here S.split ( ) method splits/breakdown the string into a list of words and is stored in a variable ‘words’. To sort those list of words we use sort( ) method. After sorting the list of words we use for loop to print the finally sorted list of words.

We can also print the sorted list of words from a sentence. This is shown in below example.

S1="VMS provides a Way to Learn new things"
words = S1.split( )
words.sort( )
print("The sorted words are:")
for word in words:
   print(word)

Output

The sorted words are:

Learn

VMS

Way

a

new

provides

things

to

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest