How to check if a character is upper-case in Python?

前端 未结 7 704
挽巷
挽巷 2020-12-08 09:18

I have a string like this

>>> x=\"Alpha_beta_Gamma\"
>>> words = [y for y in x.split(\'_\')]
>>> words
[\'Alpha\', \'beta\', \'Gam         


        
7条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 09:34

    You can use this code:

    def is_valid(string):
        words = string.split('_')
        for word in words:
            if not word.istitle():
                return False, word
        return True, words
    x="Alpha_beta_Gamma"
    assert is_valid(x)==(False,'beta')
    x="Alpha_Beta_Gamma"
    assert is_valid(x)==(True,['Alpha', 'Beta', 'Gamma'])
    

    This way you know if is valid and what word is wrong

提交回复
热议问题