0 votes
1 view
in Python by (47.8k 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 (107k 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 (22.4k 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'

Related questions

+3 votes
2 answers
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...