Need to sort the string

Description
In a certain encrypted message which has information about the location(area, city), the characters are jumbled such that first character of the first word is followed by the first character of the second word, then it is followed by second character of the first word and so on

In other words, let’s say the location is bandra,mumbai

The encrypted message says ‘bmaunmdbraai’.

Sample Input:

bmaunmdbraai

Sample Output:

bandra,mumbai

Let’s say the size or length of the two words wouldn’t match then the smaller word is appended with # and then encrypted in the above format.

With this in mind write a code to identify the right location and print it as place,city.

My Code
import ast,sys
input_str = sys.stdin.read()
#Type your code here
message1 = input_str[0::2]
message2 = input_str[1::2]
final_message = message1+’,’+message2
print(final_message)

But need to know how to remove # from the output

Say if the input is wmourmlbia#i
my solution gives worli#,mumbai
expected output should be worli,mumbai

I think you are after the replace method of string objects.

inp = input()
print((inp[0::2] + inp[1::2]).replace("#", ""))

See Built-in Types — Python 3.9.5 documentation

Header says Need to sort the string. Is it me or there is actually nothing remotely related to sorting the string? Why I bring it up? Articulating your problem in essence and correctly is part of the learning process. If you don’t know what problem you are solving there is very little chance to find a solution.

You don’t need the ast or sys modules for this task.

To get input from the user, use the input builtin function.

Well… the OP needs to reorder the string. One can contrive to do that
by sorting where the key for the sort is derived entirely from the
starting position of the character. I’m inclined to agree this isn’t
what is usually connoted by “sort”.

Cheers,
Cameron Simpson cs@cskk.id.au