Python program to sort names in alphabetical order
In this post, you will learn how to sort names in alphabetical order using the Python programming language.
Python is a high-level, open-source, general-purpose programming language. It enables us to write clear, logical applications for small and large tasks. It has a set of useful libraries, packages, and functions that minimise the use of code in our day-to-day lives.
Alphabetical order is used for organising information. It helps you to find the data quickly. You may need to add this feature to sort a list of names in organisation application development. Python provides different ways to sort a list of names in alphabetical order.
Python program to sort names in alphabetical order using sorted() function
Python provides a predefined function sorted() that returns a sorted list of the specified list. It accepts both object types, numbers and strings. Numbers are sorted numerically, and strings are sorted alphabetically. But, we can not provide a list containing both.
sorted(iterable, key, reverse)
Here, the iterable is the required parameter that contains either sort, list, dictionary, or tuple. The key is the optional parameter, which is a function to decide the order parameter. The reverse is also optional, which accepts a boolean value. False will sort ascending, True will sort descending.
In the given Python program, we have sorted names in ascending order.
names = ("Maria","Diana","Luis","Jennifer","Andrey")
sorted_names = sorted(names)
print(sorted_names)
Output of the above code:
['Andrey', 'Diana', 'Jennifer', 'Luis', 'Maria']
Python program to sort names in alphabetical order using sort() function
Python provides a predefined function sort() that sorts the items of a list in ascending or descending order. The sort() method doesn't return any value. Rather, it changes the original list.
list.sort(reverse=True|False, key=myFunc)
Here, both reverse and key are optional parameters. In the given Python program, we have sorted names in ascending order using the sort() method.
input_str = input("Enter a string: ")
# breakdown the string into a list of words
names = input_str.split()
# sort the list
names.sort()
# display the sorted names
print("The sorted names are:")
for name in names:
print(name)
Output of the above code:
Enter a string: Jorz David Cris Ana Alex
The sorted names are:
Alex
Ana
Cris
David
Jorz
Related Articles
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Replace multiple characters Python
Convert array to list Python
Python loop through list
Difference between tuple and list in Python
Convert string to list Python
Remove last element from list Python
Convert list to string Python
Convert Python list to numpy array
Remove element from list Python
Python dict inside list
Check if list is empty Python
Python iterate list with index
Python iterate list with index
Python split string by comma
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module