1

Wondering if there is a simple way to reorder characters in a string in alphabet order?

Saying, if "hello", want it to be "ehllo"? Tried sort method does not exist. If anyone have any great ideas, it will be great.

a = 'hello'
print a.sort()

thanks in advance, Lin

9
  • 1
    print ''join.sorted('hello') Commented Nov 13, 2015 at 23:55
  • 1
    look at the link. it makes use of str.join(list) and sorted(iterable) methods Commented Nov 13, 2015 at 23:58
  • 1
    no those are both built in methods Commented Nov 13, 2015 at 23:58
  • 1
    I think the first comment had a typo and was supposed to be print(''.join(sorted("hello"))) Commented Nov 14, 2015 at 0:03
  • 1
    @Galax, yes, that is my bad. because it is python 2, the out most brackets are not necessary but the call to join should be in brackets Commented Nov 14, 2015 at 0:04

1 Answer 1

3

Try this:

a = 'hello'
a = ''.join(sorted(a))
print a

It should return 'ehllo'.

Here's a similar post about the same problem: How to sort the letters in a string alphabetically in Python

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks user1159580, smart answer!
@LinMa: if it helped you, could you accept it?
@jermenkoo, good question, will definitely accept. Done. :)
Thanks Brian! Mark as answered and have a good weekend.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.