2

How to sort the letters in a string alphabetically in Python without using join()? Using join() we can do this:

def sort_string(a)
    return ''.join(sorted(a))

Is there a way to sort string without using join()?

  • You can perform the join manually one letter at a time, but why wouldn't you want to use `join`? – interjay Nov 14 '15 at 10:24
  • 2
    Possible duplicate of [How to sort the letters in a string alphabetically in Python](http://stackoverflow.com/questions/15046242/how-to-sort-the-letters-in-a-string-alphabetically-in-python) – Michelle Welcks Nov 14 '15 at 10:39
  • @binarysubstrate that gives the answer he starts with. Of course, I agree with interjay: why don't you want to use join? That is a very pythonic way of doing it. – Foon Nov 14 '15 at 13:42
  • I'm voting to close this question as off-topic because use `str.join` here is the best solution. – Remi Guan Nov 15 '15 at 01:27

1 Answers1

4

Your solution is correct as strings are immutable in python. So, it's impossible to change (in your case - sort) an existing string. You have to create new one (you do it with join() call).

Also, good notes about sorting letters in string in python can be found here: How to sort the letters in a string alphabetically in Python

Community
  • 1
  • 1
Dmitry
  • 432
  • 2
  • 6