How to check if characters in a string are alphabetically ordered

后端 未结 5 524
面向向阳花
面向向阳花 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条回答
  •  清歌不尽
    2021-01-26 08:46

    is is identity testing which compares the object IDs, == is the equality testing:

    In [1]: s1 = "Hello World"
    In [2]: s2 = "Hello World"
    
    In [3]: s1 == s2
    Out[3]: True
    
    In [4]: s1 is s2
    Out[4]: False
    

    Also note that sorted returns a list, so change it to:

    if ''.join(s2) == s1:
    

    Or

    if ''.join(sorted(s2)) == s1:
    

提交回复
热议问题