小编典典

如何在Python中检查字符是否为大写?

python

我有这样的字符串

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

我想要输出说X不符合,因为列表单词的第二个元素以小写开头,如果字符串x = "Alpha_Beta_Gamma"则应打印字符串符合


阅读 216

收藏
2020-12-20

共1个答案

小编典典

也许你想要
str.istitle

>>> help(str.istitle)
Help on method_descriptor:

istitle(...)
    S.istitle() -> bool

    Return True if S is a titlecased string and there is at least one
    character in S, i.e. uppercase characters may only follow uncased
    characters and lowercase characters only cased ones. Return False
    otherwise.

>>> "Alpha_beta_Gamma".istitle()
False
>>> "Alpha_Beta_Gamma".istitle()
True
>>> "Alpha_Beta_GAmma".istitle()
False
2020-12-20