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

前端 未结 7 693
挽巷
挽巷 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:35

    You can use this regex:

    ^[A-Z][a-z]*(?:_[A-Z][a-z]*)*$
    

    Sample code:

    import re
    
    strings = ["Alpha_beta_Gamma", "Alpha_Beta_Gamma"]
    pattern = r'^[A-Z][a-z]*(?:_[A-Z][a-z]*)*$'
    
    for s in strings:
        if re.match(pattern, s):
            print s + " conforms"
        else:
            print s + " doesn't conform"
    

    As seen on codepad

提交回复
热议问题