Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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'

Related questions

+3 votes
2 answers
0 votes
4 answers
0 votes
4 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...