Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Sắp xếp chữ cái trong chuỗi theo alphabet Python

Để sort chữ cái trong string alphabetically, dùng function sorted trả về sorted list chữ cái rồi join thành string. Ví dụ:

>>> s = "helloworld"
>>> ''.join(sorted(s))
'dehllloorw'

Muốn chỉ unique character sorted, dùng set. Ví dụ:

>>> s = "helloworld"
>>> ''.join(sorted(set(s)))
'dehlorw'