Python 判断字符串是否为大写及 is 方法延伸

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()

如果你依然在编程的世界里迷茫,不知道自己的未来规划,对python感兴趣,可以来我的Python学习交流群:556370268,这里是python学习者聚集地,里面都是学习python的,从最基础的python【python,游戏,网络安全,数据挖掘,爬虫】到网络安全的项目实战的学习资料都有整理,希望能帮助你更了解python,学习python。

 

猜你喜欢

转载自blog.csdn.net/weixin_44369414/article/details/89675544