Python 判断字符串是否为大写及延伸
以下方法仅判断字符,数字和符号不影响结果
isupper()判断是否都为大写
>>> '1a!'.isupper()
False
>>> '1A!'.isupper()
True
islower()判断是否都为小写
>>> '1As!'.islower()
False
>>> '1s!'.islower()
True
istitle()判断所有的单词首字符都是大写
>>> 'This Is Upper'.istitle()
True
>>> 'This Is upper1'.istitle()
False
isspace()判断所有的字符都是空格
>>> 'This Is upper1'.isspace()
False
>>> ' '.isspace()
True
isalnum()判断所有的字符都是数字或字母
>>> '1a!'.isalnum()
False
>>> '1a'.isalnum()
True
>>> 'aa'.isalnum()
True
>>> '11'.isalnum()
True
isalpha()判断所有的字符都是字母
>>> '11'.isalpha()
False
>>> '1a'.isalpha()
False
>>> 'aa'.isalpha()
True
isdigit()判断所有的字符都是数字
>>> 'aa'.isdigit()
False
>>> '11'.isdigit()
True
>>> '1a'.isdigit()
False
还有两个类似的方法 isdecimal() 和 isnumeric()
最近更新
最近热读
扫码关注公众号,或搜索公众号“温欣爸比” 及时获取我的最新文章