使用join函数输出会更方便一点 while 1: try: strlist=input().split(',') strlist.sort() print(','.join(strlist)) except: break