How to check if characters in a string are alphabetically ordered

后端 未结 5 571
面向向阳花
面向向阳花 2021-01-26 08:18

I have been trying these code but there is something wrong. I simply want to know if the first string is alphabetical.

def alp(s1):
    s2=sorted(s1)
    if s2 i         


        
5条回答
  •  萌比男神i
    2021-01-26 08:40

    use something like this:

    sorted() returns a list and you're trying to compare a list to a string, so change that list to a string first:

    In [21]: "abcd"=="".join(sorted("abcd"))
    Out[21]: True
    
    In [22]: "qwerty"=="".join(sorted("qwerty"))
    Out[22]: False
    
    #comparsion of list and a string is False
    In [25]: "abcd"==sorted("abcd")
    Out[25]: False
    

提交回复
热议问题