Sort List of Strings in Python (2 Examples)


In this tutorial, I’ll demonstrate how to sort a list of strings in the Python programming language.

The table of content looks as follows:

Let’s dive into it!


Example Data

At the start, we’ll need to create some data that we can use in the following examples. It is a Python list containing six strings.

string_list = ["Statistics", "Data", "Science", 
                "Python", "String", "Hello"]    # generating an example float list


Example 1: Sorting using sort() Function

Example 1 illustrates how to sort a list of strings in ascending order using the sort() function.

string_list.sort()                              # sorting the list ascending
print(string_list)                              # printing the sorted list
# ['Data', 'Hello', 'Python', 'Science', 'Statistics', 'String']

Notice that the list was sorted in place, which means that a new object was not created, instead, string_list became sorted. As an alternative, the list can also be sorted in descending order by setting the reverse argument to True, see below.

string_list.sort(reverse=True)                  # sorting the list descending
print(string_list)                              # printing the sorted list
# ['String', 'Statistics', 'Science', 'Python', 'Hello', 'Data']

Now you see that the order of the elements in string_list has been reversed.


Example 2: Sorting using sorted() Function

Example 2 explains how to sort a list of strings with the use of the sorted() function.

print(sorted(string_list))                      # sorting the list and printing
# ['Data', 'Hello', 'Python', 'Science', 'Statistics', 'String']

The unique thing about the sorted() function is that it generates a new sorted list object, which contains the elements of the original list in an ordered way. See how printing string_list returns the unordered result in the beginning.

['Statistics', 'Data', 'Science', 'Python', 'String', 'Hello']

Now let’s order the list in reversed order. Just like in example 1, the reverse argument helps to implement it.

print(sorted(string_list, reverse=True))        # sorting in reverse and printing
# ['String', 'Statistics', 'Science', 'Python', 'Hello', 'Data']

Here you go, now the elements are in reverse order.


Video & Further Resources

I have recently released a video on my YouTube channel, which shows the Python syntax of this article. You can find the video below:



Besides the video, you could have a look at the other tutorials on this website. A selection of articles that are related to the sorting of a list of strings is shown below.


In summary: At this point, you should know how to create a sorted list of strings in Python programming. Don’t hesitate to let me know in the comments section if you have additional questions or comments.


Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.