Updated April 12, 2023
Introduction to Sort string in Python
The sort string in python is used to sort or order the characters of the string in alphabetical order. The python provides the built in methods to sort the string. Sorting string has always been popular utility with lots of applications everywhere, as sometimes we need to sort data based on customer name either in ascending order or descending order, so python provided the sort methods for this purpose. There are two methods sort() and sorted() which are used to sort containers in python.
The sort() method modifies the list in-place, where as the sorted() method create a new sorted list from an original list. All the containers in python are not mutable like string, so the sort() method does not work with it because sort function in place tries to sort and change the order which does not allow because of immutability. As the sorted() creates a new sorted list so it must sort the string.
Syntax of python sort string sorted() method:
sorted(iterable, key = key, reverse = reverse)
Parameters:
- iterable: This is not an optional parameter, it is a sequence of string to be sort.
- key: This is an optional parameter, which specify the function to be execute to specify the order of sort. The default value is None.
- reverse: This is an Boolean optional parameter, which specify the order to be ascending or descending. False specify ascending order, True specify descending order. The default value is False.
Return Value:
The return value of this method is the sorted string.
Working of Sort string in Python
The sort string in python accepts a string as a parameter, then each character of the string compare based on the ASCII value of the characters and then return the list of sorted characters of the string.
Examples of Sort string in Python
Given below are the examples mentioned:
Example #1
Example for sorting the string using python built in sorted() method.
Code:
# creating string
msg = "Hello!, how are you"
print("The original message is : ")
print(msg)
# using sorted() method to get list of sorted characters
alt_msg = sorted(msg)
print( "The output of the sorted() method is : " )
print( alt_msg )
# using join() method to join list of sorted characters
sort_msg = "".join(alt_msg)
print( "The sorted message is : " )
print( sort_msg )
Output:
As in the above program the one string variable is created which store the string “Hello!, how are you”. Next in the program the sorted() method is used which return the sorted list of string characters (as displaying in the output as well). Then the sorted list of characters are joined with the help of join() method, which return the string of list of sorted characters. So the final sorted string contain all the characters as the original string but in sorting order of characters. The sorted string have space as first character because its ASCII value is lower than all other characters in the string.
Example #2
Example for sorting the string using while loop in python.
Code:
# creating string
string = "Sample string to sort alphabetical order."
print( "The original string is : " )
print( string )
# using sorted() method to get list of sorted characters with ascending order
list_string=list( string )
print( "The list of string is : " )
print( list_string )
list_string = list(string)
i = 0
while i < len( list_string ):
key = i
j = i+1
while j < len( list_string ): if list_string[key] > list_string[j]:
key = j
j += 1
list_string[i],list_string[key] = list_string[key],list_string[i]
i += 1
print( "The list of sorted string is : " )
print( list_string )
new_string = "".join(list_string )
print( "The sorted string is : " )
print(new_string)
Output:
As in the above program the string “Sample string to sort alphabetical order.” Is sorting without using the sorted() method. As in the above program with the help of while loop the each character is comparing, the first while loop checking that the loop iteration should be less than length of the string and taking in sequence one by one element, which are comparing and swapping in the second while loop. So, it return sorted list of string characters. Then the sorted list of characters are joined by join() method. So the final sorted string contain all the characters as the original string but in sorting order of characters.
Example #3
Example for sorting the string using for loop in python which is also a implementation of bubble sort.
Code:
# creating string
string = "This is a sample string"
new_string = []
print( "The original string is : " )
print( string )
# woith out using sorted() method to get list of sorted characters
list_string=list(string)
print( "The list of string is : " )
print(list_string)
list_string=list(string)
len_s=len(list_string)
for i in range(len_s-1):
for j in range(len_s-i-1):
if list_string[j]>list_string[j+1]:
list_string[j],list_string[j+1]=list_string[j+1],list_string[j]
for m in list_string:
new_string+=m
print( "The list of sorted string is : " )
print(new_string)
sort_string = "".join(new_string)
print( "The sorted string is : " )
print(sort_string)
Output:
As in the above program the string “This is a sample string” is sorting without using the sorted() method. In the program the with the help of for loop each character is comparing and if the previous character is greater than next character then swap is performing else no swapping. So, it return sorted list of string characters (as displaying in the output as well). Then the sorted list of characters are joined with the help of join() method, which return the string of ascending. So the final sorted string contain all the characters as the original string but in sorting order of characters.
Conclusion
The Sort string in Python is used to sort the characters of the string in alphabetical order, which can be perform by sorted() and join() method.
Recommended Articles
We hope that this EDUCBA information on “Sort string in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.