如何在 Python 中检查字符是否为大写? [英] How to check if a character is upper-case in Python?
本文介绍了如何在 Python 中检查字符是否为大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的字符串
<预><代码>>>>x="Alpha_beta_Gamma">>>words = [y for y in x.split('_')]>>>字['Alpha'、'beta'、'Gamma']我想要输出说 X 不符合,因为列表单词的第二个元素以小写开头,如果字符串 x = "Alpha_Beta_Gamma"
那么它应该打印字符串是符合的>
解决方案
也许你想要 str.istitle
I have a string like this
>>> x="Alpha_beta_Gamma"
>>> words = [y for y in x.split('_')]
>>> words
['Alpha', 'beta', 'Gamma']
I want output saying X is non conformant as the the second element of the list words starts with a lower case and if the string x = "Alpha_Beta_Gamma"
then it should print string is conformant
解决方案
Maybe you want 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
这篇关于如何在 Python 中检查字符是否为大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文