Check if all characters of a string are uppercase

前端 未结 5 971
遥遥无期
遥遥无期 2021-01-03 23:21

Say I have a string that can contain different characters:

e.g. word = "UPPER£CASe"

How would I test the string to see if all the charac

5条回答
  •  臣服心动
    2021-01-03 23:56

    You can alternatively work at the level of characters.

    The following function may be useful not only for words but also for phrases:

    def up(string):
        upper=[ch for ch in string if ch.isupper() or ch.isspace()]
        if len(upper)==len(string):
            print('all upper')
        else:
            print("some character(s) not upper")
    
    strings=['UPPERCAS!', 'UPPERCASe', 'UPPERCASE', 'MORE UPPERCASES']
    for s in strings:
       up(s)
    Out: some character(s) not upper 
    Out: some character(s) not upper
    Out: all upper
    Out: all upper
    

提交回复
热议问题