Example HTML page

A python program to sort a group of literals

In this post, i will show you how to sort group of literals. In general literals are the combination of strings, integers, float values and so on. The below program describes the sorting of literals.

#Program

L=[ ]
num=int(input("number of literals: "))
for x in range(num):
  T=input("Text is: ")
  L.append(T)
L.sort( )
for b in L:
  print(b)

Output

#sample 1

number of literals: 3

Text is: Sudhakar

Text is: Akhil

Text is: Ram

Akhil

Ram

Sudhakar

 

#sample 2

number of literals: 5

Text is: 16

Text is: 8C

Text is: are

Text is: their

Text is: id’s

16

8C

are

id’s

their

Explanation:

L=[ ], creates an empty list which can stores the combinations of different literals(like strings, integers, float values and so on). Next read the number of literals from user and is stored in “num” variable. Perform looping on every element in the list ‘L’. Now the user input can be read and is stored in “T” variable. L.appened( ) is one of the methods to append the elements in to a list and sort( ) method sorts out the literals in a specific order. Now with the help of for loop print the elements of list ‘L’ again.

Example HTML page

Leave a Reply

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

Pin It on Pinterest