python怎么判断大小写

Python (218) 2023-04-14 18:06:00

Python提供了isupper(),islower(),istitle()方法用来判断字符串的大小写,具体实例如下:

>>>str_1="HELLOPYTHON"#全大写
>>>str_2="HelloPYTHON"#大小写混合
>>>str_3="HelloPython"#单词首字母大写
>>>str_4="hellopython"#全小写

isupper() 判断是否全是大写

>>>str_1.isupper()
True
>>>str_2.isupper()
False
>>>str_3.isupper()
False
>>>str_4.isupper()
False

相关推荐:《Python入门教程》

islower() 判断是否全是小写

>>>str_1.islower()
False
>>>str_2.islower()
False
>>>str_3.islower()
False
>>>str_4.islower()
True

istitle() 判断首字母是否大写,其余的是否小写

>>>str_1.istitle()
False
>>>str_2.istitle()
False
>>>str_3.istitle()
True
>>>str_4.istitle()
False
THE END

发表回复