Open In App

Python Program to Sort a String

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Sorting strings in Python is a common and important task, whether we need to organize letters alphabetically or systematically handle text data. In this article, we will explore different methods to sort a string starting from the most efficient to the least.

Using sorted with join()

sorted() function is the most efficient way to sort a string. It returns a sorted list of characters which we can then join back into a single string. This method is concise and efficient as it uses Python's built-in functions.

Python
s = "python"

# Sorting the string
sorted_string = ''.join(sorted(s))
print(sorted_string)

Output
hnopty

Explanation:

  • sorted(text) creates a list of sorted characters from the input string.
  • ' '.join() combines the sorted characters back into a single string.

Let's explore different ways to sort a string in Python.

Using a Custom Sorting Function

If we need more control over the sorting logic, we can use a custom key function with sorted().

Python
s = "python"

# Custom sorting: reverse alphabetical order
sorted_string = ''.join(sorted(s, reverse=True))
print(sorted_string)

Output
ytponh

Explanation:

  • reverse=True sorts the characters in descending order.
  • Customization Allows sorting based on specific criteria.
  • This method is slightly less efficient due to the custom logic but still uses sorted() efficiently.

Using a For Loop

If we want a manual approach, sorting can be implemented using loops. This is less efficient but helps understand the logic behind sorting.

Python
s = "python"

# Sorting manually
sorted_list = list(s)
for i in range(len(sorted_list)):
    for j in range(i + 1, len(sorted_list)):
        if sorted_list[i] > sorted_list[j]:
            sorted_list[i], sorted_list[j] = sorted_list[j], sorted_list[i]

sorted_string = ''.join(sorted_list)
print(sorted_string)

Output
hnopty

Explanation:

  • Nested loops compare and swap characters to arrange them in order.
  • Manual sorting helps understand the sorting process but is less efficient.

Using a Temporary Data Structure

We can use additional data structures, such as dictionaries or counters, to assist in sorting. This approach can be useful in specific scenarios.

Python
s = "python"

# Sorting using a dictionary
char_count = {char: s.count(char) for char in s}
sorted_string = ''.join(sorted(char_count.keys()))
print(sorted_string)

Output
hnopty

Explanation:

  • text.count(char) counts occurrences of each character.
  • Sorting keys Sorts characters based on their occurrences or order in the dictionary.
  • This method is less efficient due to the overhead of counting characters.

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy