题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
Note new functions:
- isalpha(): determine whether all English characters
- sorted(xxx,
key = str.upper
): sort by uppercase
while True:
try:
ipt = input()
# 1. sort out all characters
a = ''
for i in ipt:
# add to new string only if it is an English character
if i.isalpha():
a += i
b = sorted(a, key = str.upper)
# 2. add other non-characters
index = 0
c = ''
for i in range(0, len(ipt)):
if ipt[i].isalpha():
c += b[index]
index += 1
else:
c += ipt[i]
print(c)
except:
break