In this tutorial, I will explain how to sort a string in Python. As a Python developer working on a logistics application for a company in Los Angeles, I encountered a requirement to alphabetically sort location codes stored as strings. Sorting these codes was critical to ensure efficient searching and organization. Let us explore how to sort a string in Python effectively.
Sort a String in Python
Python provides various ways to sort strings in Python. Let us see some important methods to achieve this task.
Read Find the First Number in a String in Python
Method 1. Use the sorted() Function
Python provides a built-in sorted() function that allows you to sort strings easily. The sorted() function returns a new sorted list from the given iterable. Here’s an example:
names = ["John", "Alice", "Michael", "Emma", "Daniel"]
sorted_names = sorted(names)
print(sorted_names)Output:
['Alice', 'Daniel', 'Emma', 'John', 'Michael']I have executed the above example code and added the screenshot.

In this example, we have a list of common names in the USA. By passing the names list to the sorted() function, we obtain a new list sorted_names with the names sorted in alphabetical order.
Read How to Compare Strings in Python?
Method 2. Use the sort() Method
Another way to sort Python strings is by using the sort() method directly on a list. Unlike sorted() , the sort() method modifies the original list in place. Here’s an example:
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
cities.sort()
print(cities)Output:
['Chicago', 'Houston', 'Los Angeles', 'New York', 'Phoenix']I have executed the above example code and added the screenshot.

In this case, we have a list of major cities in the USA. By calling the sort() method on the cities list, the list itself is sorted alphabetically.
Read How to Create a String of N Characters in Python?
Method 3. Use the join() Method
You can sort the characters of a string by first converting it to a Python list, sorting the list, and then joining the sorted characters back into a string. This can be particularly useful when you need to rearrange characters in a specific order, such as sorting alphabetically or in reverse order
string = "California"
sorted_string = ''.join(sorted(string))
print( sorted_string)Output:
CaafiilnorI have executed the above example code and added the screenshot.

The join() method is used to concatenate the sorted characters back into a single string.
Read How to Split a String into Equal Parts in Python?
Sort Strings in Reverse Order
To sort Python strings in reverse alphabetical order, you can pass the reverse=True parameter to either the sorted() function or the sort() method. Here’s an example:
states = ["California", "Texas", "Florida", "New York", "Illinois"]
states.sort(reverse=True)
print(states)Output:
['Texas', 'New York', 'Illinois', 'Florida', 'California']I have executed the above example code and added the screenshot.

Here, we have a list of populous states in the USA. By setting reverse=True , the sort() method sorts the states in reverse alphabetical order.
Read How to Insert a Python Variable into a String?
Sort Strings Case-Insensitively
By default, string sorting in Python is case-sensitive, meaning uppercase letters come before lowercase letters. However, you can perform a case-insensitive sort using the str.lower or str.upper function as the key parameter. Here’s an example:
presidents = ["George Washington", "John Adams", "Thomas Jefferson", "james madison", "James Monroe"]
presidents.sort(key=str.lower)
print(presidents)Output:
['George Washington', 'James Monroe', 'james madison', 'John Adams', 'Thomas Jefferson']In this example, we have a list of early US presidents. By specifying key=str.lower , the sort() method converts each string to lowercase before sorting, resulting in a case-insensitive alphabetical order.
Read How to Split a String by Index in Python?
Sort Strings by Length
You can also sort Python strings based on their length using the len function as the key parameter. Here’s an example:
rivers = ["Mississippi", "Missouri", "Colorado", "Ohio", "Columbia"]
rivers.sort(key=len)
print(rivers)Output:
['Ohio', 'Colorado', 'Missouri', 'Columbia', 'Mississippi']In this case, we have a list of major rivers in the USA. By setting key=len , the sort() method sorts the rivers based on their length, from shortest to longest.
Read Convert Binary to Decimal in Python
Custom Sorting Strings
For more advanced sorting scenarios, you can define your custom sorting function and pass it as the key parameter. This allows you to sort strings based on specific criteria. Here’s an example:
def last_name(name):
return name.split()[-1]
authors = ["Mark Twain", "Edgar Allan Poe", "Emily Dickinson", "William Faulkner", "Ernest Hemingway"]
authors.sort(key=last_name)
print(authors)Output:
['Emily Dickinson', 'William Faulkner', 'Ernest Hemingway', 'Edgar Allan Poe', 'Mark Twain']In this example, we have a list of famous American authors. We define a custom function last_name() that takes a full name and returns the last name. By passing last_name as the key parameter, the sort() method sorts the authors based on their last names.
Read How to Split a String Every N Characters in Python?
Conclusion
In this tutorial, I have explained how to sort a string in Python. I explained various methods like using the sorted() function, sort() method, and join() method. I also discussed how to sort strings in reverse order, case-insensitively, sort by length and custom sorting.
You may also like to read:
- Python Split Regex
- How to Split a Sentence into Words in Python?
- How to Split a String and Get the Last Element in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.