In this tutorial you will learn about the Python Program to Sort Words in Alphabetic Order and its application with practical example.
In this tutorial, we will learn to create a Python Program to Sort Words in Alphabetic Order using python Programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Python programming topics:
- Python Operator.
- Basic Input and Output
- Basic Python programming.
- Python Data type.
- Python in-built Functions.
- Loops in Python.
- List in python.
- Sting in python.
Lexicographical (Alphabetic order).
Lexicographic or Lexicographical sequence is a normally the Alphabetical order( Ascending order) Sequences of ordered letters ,String or elements in a ordered set.
Python Program to Sort Words in Alphabetic Order
In this program we will create a Python program to sort “Alphabetically” or in “Ascending” order .We would first declared and initialized the required variables. Next, we would prompt user to input the values later we will sort a string in Alphabetic Order.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#Program to sort alphabetically the words form a string provided by the user # taking value from user.. Strng = input("Enter a string: ") # breaking the string into list of words words = Strng.split() # sorting the list words.sort() # displaying the sorted words list for word in words: print(word) |
Output
In our program above we going to sort entered list of word in “Alphabetical” order. within the program first we break string in to word and with “sort” function sort the word in Lexicographic order.
In the above program, we have first declared and initialized a set variables required in the program.
- Strng = it will hold entered string.
- words=it will hold hold word.
- word= gives sorted words.
.
After taking string from user we pass this string to function where we break the string to words using split() method.
We take a String form the user and using the split() method string breakdown into a list of words.The function split(), splits the string at “white-spaces”.
So the elements of List(words) will be sorted using the “sort()” in-built method As show in image above and all the words are display as shown in here.