如何在 Python 中检查字符是否为大写? [英] How to check if a character is upper-case in Python?

查看:33
本文介绍了如何在 Python 中检查字符是否为大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串

<预><代码>>>>x="Alpha_beta_Gamma">>>words = [y for y in x.split('_')]>>>字['Alpha'、'beta'、'Gamma']

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

解决方案

也许你想要 str.istitle

<预><代码>>>>帮助(str.istitle)关于 method_descriptor 的帮助:标题(...)S.istitle() ->布尔值如果 S 是标题字符串并且至少有一个,则返回 TrueS 中的字符,即大写字符只能跟在无大小写的后面字符和小写字符只能区分大小写.返回错误除此以外.>>>"Alpha_beta_Gamma".istitle()错误的>>>"Alpha_Beta_Gamma".istitle()真的>>>"Alpha_Beta_GAmma".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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆