How to sort letters in a string alphabetically in Python

Is there an easy way to sort letters in a string alphabetically in Python?

So for:

a = 'ZENOVW' 

I would like to return:

 'ENOVWZ' 
+127
python string
Feb 23 '13 at 22:00
source share
9 answers

You can do:

 >>> a = 'ZENOVW' >>> ''.join(sorted(a)) 'ENOVWZ' 
+217
Feb 23 '13 at 22:02
source share
 >>> a = 'ZENOVW' >>> b = sorted(a) >>> print b ['E', 'N', 'O', 'V', 'W', 'Z'] 

sorted returns a list, so you can again create a string using join :

 >>> c = ''.join(b) 

which connects the elements of b along with an empty string '' between each element.

 >>> print c 'ENOVWZ' 
+78
Feb 23 '13 at 22:07
source share
Decision

Sorted () may give you unexpected results with other lines.

List of other solutions:

Sort letters and their difference:

 >>> s = "Bubble Bobble" >>> ''.join(sorted(set(s.lower()))) ' belou' 

Sort letters and their selection during storage:

 >>> s = "Bubble Bobble" >>> ''.join(sorted(set(s))) ' Bbelou' 

Sort letters and save duplicates:

 >>> s = "Bubble Bobble" >>> ''.join(sorted(s)) ' BBbbbbeellou' 

If you want to get rid of the white space as a result, add the strip () function in any of the above cases:

 >>> s = "Bubble Bobble" >>> ''.join(sorted(set(s.lower()))).strip() 'belou' 
+26
Oct 17 '15 at 18:48
source share

You can use abbreviation

 >>> a = 'ZENOVW' >>> reduce(lambda x,y: x+y, sorted(a)) 'ENOVWZ' 
+7
Nov 24 '17 at 12:34 on
source share

code can be used to sort a string alphabetically without using the python built-in function

k = input ("Enter any line again")

 li = [] x = len(k) for i in range (0,x): li.append(k[i]) print("List is : ",li) for i in range(0,x): for j in range(0,x): if li[i]<li[j]: temp = li[i] li[i]=li[j] li[j]=temp j="" for i in range(0,x): j = j+li[i] print("After sorting String is : ",j) 
+1
Jun 11 '18 at 16:18
source share

The sorted Python function returns the ASCII result for a string.

INCORRECT : in the example below, e and d are behind H and W due to the ASCII value.

 >>>a = "Hello World!" >>>"".join(sorted(a)) ' !!HWdellloor' 

CORRECT : to write a sorted string without changing the case of letters. Use code:

 >>> a = "Hello World!" >>> "".join(sorted(a,key=lambda x:x.lower())) ' !deHllloorW' 

If you want to delete all punctuation marks and numbers. Use code:

 >>> a = "Hello World!" >>> "".join(filter(lambda x:x.isalpha(), sorted(a,key=lambda x:x.lower()))) 'deHllloorW' 
+1
Apr 14 '19 at 23:42
source share

Loved the answer using the redu () function. Here's another way to sort a string using the accumulate () method.

 from itertools import accumulate s = 'mississippi' print(tuple(accumulate(sorted(s)))[-1]) 

sorted (s) → ['i', 'i', 'i', 'i', 'm', 'p', 'p', 's', 's', 's', 's']

tuple (accumulate (sorted) (s)) → ('i', 'ii', 'iii', 'iiii', 'iiiim', 'iiiimp', 'iiiimpp', 'iiiimpps',' iiiimppss', 'iiiimppsss ',' iiiimppssss')

We select the last index (-1) of the tuple

0
Sep 04 '18 at 10:21
source share

yes the fruit does not help

0
Feb 05 '19 at
source share
 x='hello' d=list(x) d.sort() print("".join(d)) 
0
Apr 20 '19 at 10:11
source share



All Articles