Python program to arrange names in alphabetical order

Write a python program to arrange the names in alphabetical order. In this module, we are going to learn how to sort and arrange the names in an alphabetical order

Sample test 1

Input:  Zainab, Ayush, Neha, Alex, Mitchell

Output: Alex, Ayush, Mitchell, Neha, Zainab

sample test 2

Input: shivam, raj , ravi, ayush, neha, naved,ali, kesav, pihu,mohini

Output: ali ,ayush, kesav, mohini, naved, neha, pihu, raj, ravi, shivam

Algorithm for code

  • Take input about how many names user inputting
  • Create a list add the name
  • Sort the elements of the list
  • Print the output

Python program to arrange names in alphabetical order

total_name=int(input("Total name wants to input:"))
list_of_names=[]
for i in range(total_name):
    name=input("Enter the name:")
    list_of_names.append([name])
print("Names given are:",list_of_names)
sorted_name=[x[0] for x in list_of_names]
sorted_name.sort()
for j in sorted_name:
    print(j)
    

Explanation of the code

Step 1: we took input for the user about the number of names inputted

Step 2: Creating a list for further uses

Step 3: using for loop to run the code multiple times

Step 4: taking input of name

Step 5: appending the name to a list here we use square brackets inside parenthesis because when we access them one by one it will only access the first letter of the name

Step 6: now we create another list but use the direct method and add elements from one list to another list

Step 7: now sorting the list

Step 8:printing the element by accessing one by one using the for loop again

Output

 

In this module, we have learned how to create a program to sort out the names according to the alphabetic order