0 votes
1 view
in Python by (30.9k points)

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

So for:

a = 'ZENOVW'

I would like to return:

'ENOVWZ'

2 Answers

0 votes
by (73.5k points)

To sort the letters in a string alphabetically in Python you can do something as follows:-

a = 'ZENOVW' 

''.join(sorted(a)) 

'ENOVWZ'

0 votes
by (20.3k points)

Use the code given below:

>>> a = 'ZENOVW'

>>> b = sorted(a)

>>> print b

['E', 'N', 'O', 'V', 'W', 'Z']

And the sorted returns a list, so you can just make it a string again using join like this:

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

Which will join the items of b together with an empty string '' in between each item.

>>> print c

'ENOVWZ'

...