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()?

4
  • You can perform the join manually one letter at a time, but why wouldn't you want to use join? Commented Nov 14, 2015 at 10:24
  • 2
    Possible duplicate of How to sort the letters in a string alphabetically in Python Commented Nov 14, 2015 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. Commented Nov 14, 2015 at 13:42
  • I'm voting to close this question as off-topic because use str.join here is the best solution. Commented Nov 15, 2015 at 1:27

1 Answer 1

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.