First of all, remembering that python has much more efficient sorting algorithms already implemented in the language, just call a function to sort:
entrada = ''.join(sorted(entrada))
But, answering your question: yes, lists are mutable, you can convert your string to list:
entrada = "32415"
entrada = list(entrada)
Your sorting algorithm has some problems, it looks like you're trying to use the "bubble method", however you need to get back to wherever there is a trade, in which case it would be best to use while
instead of for
:
i = 1
while i < len(entrada):
if entrada[i] < entrada[i-1]:
entrada[i], entrada[i-1] = entrada[i-1], entrada[i]
i = max(1, i - 1)
else:
i += 1
Then in the end you can convert the result back to string using join
:
entrada = ''.join(entrada)
print(entrada)
The result:
12345