可以使用sorted函數對字符串進行降序排序,然后使用join方法將排好序的字符重新組合成一個字符串。示例如下:
s = "hello world" sorted_s = ''.join(sorted(s, reverse=True)) print(sorted_s)
輸出結果為:
wroolldlh e