Thursday 13 August 2020

Sort characters in a string

 Assume input string contains only alphabet symbols and digits.

We need to write a program to sort characters of the string were we will sort alphabets first and then digits will follow

Sample Input: C8N9A2U6R5B7

Output: ABCNRU256789

string_data=input("Enter some alphanumeric string to sort: ")
alphabets=[]
digits=[]
for ch in string_data:
	if ch.isalpha():
		alphabets.append(ch)
	else:
		digits.append(ch)
result=''.join(sorted(alphabets)+sorted(digits))
print("You entered: ",string_data)

print("Final result after Sorting: \n", result )

No comments:

Post a Comment