题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
def str_sort(s): N = len(s) res = [] letters = [] for i in s: # 判断是否是字母,可用if 'a' <= i.lower() <= 'z'代替,下面的同上 if i.isalpha(): letters.append(i) # 忽略大小写的排序 sorted_letters = sorted(letters, key=str.lower) index = 0 for i in range(N): if s[i].isalpha(): res.append(sorted_letters[index]) index += 1 else: res.append(s[i]) return "".join(res) if __name__ == "__main__": while True: try: s = input() print(str_sort(s)) except: break