- ·上一篇内容:Python isdigit()函数
- ·下一篇内容:Python isnumeric()函数的使用
Python islower()函数 判断字符串中字符是否都为小写
Python中提供了一系列的字符串处理函数可以帮我们解决实际中的很多问题。
islower()函数是Python中的另外一个内建字符串处理函数,其作用是判断一个字符串中的所有字母字符是否都为小写拼写形式,如果是则返回True,否则返回False.
一、islower()的语法格式
str.islower()
其中,str是要被判断的字符串或字符串变量;
该函数没有参数;
该函数的返回值为逻辑值:True 或 False.
二、islower()函数使用示例
1、字符串中仅含小写字母
str1 = "hello world!"
print(str1.islower())
输出:True
2、字符串中仅含小写字母及其他非字母形式
str1 = "翔宇亭it乐园"
print(str1.islower())
str1 = "hello, @134的小朋友"
print(str1.islower())
输出:
True
True
以上三例在Python3.8.2中的执行情况如下图所示:
3、字符串中含有大写字母
str1 = "Hello,world!"
print(str1.islower())
str1 = "Hello, World!"
print(str1.islower())
str1 = "HELLO,WORLD!"
print(str1.islower())
str1 = "翔宇亭IT乐园"
print(str1.islower())
str1 = "Hello,@Kitty"
print(str1.islower())
以上在Python3.8.2中的运行情况如下图所示:
4、其他语言的字母
str1 = "κμνξ" #希腊字母
print(str1.islower())
str1 = "я тебя люблю" #俄语字母
print(str1.islower())
上面程序中使用了希腊字母和俄语字母的小写形式,所以输出都为True.
以上在Python3.8.2中的运行情况如下图所示:
5、字符串是空字符串或不含字母的情况
str1 = "" #空字符串
print(str1.islower())
str1 = "@#@" #特殊字符
print(str1.islower())
str1 = "202006" #仅含数字
print(str1.islower())
以上在Python3.8.2中的运行情况如下图所示:
使用下面的程序,可以输出Unicode编码中的所有小写字母形式:
import unicodedata
total_count = 0
for i in range(2 ** 16):
charac = chr(i)
if charac.islower():
print(u'{:04x}: {} ({})'.format(i, charac, unicodedata.name(charac, 'UNNAMED')))
total_count = total_count + 1
print("所有小写Unicode 小写形式的字符数量为:", total_count)
三、总结
如果字符串中没有包含任何字母形式的字符,islower()函数返回False;
如果字符串中包含字母,且所有字母都是小写形式的,islower()函数返回True;
islower()函数不仅能判断英文字母,也能判断其它语言中的字母形式;
如果字符串为空字符串,则返回False.
如需转载请注明出处:翔宇亭IT乐园(http://www.biye5u.com)
本文链接地址:
欢迎留言评论,共议Python大事。
微信搜索“优雅的代码”关注本站的公众号,或直接使用微信扫描下面二维码关注本站公众号,以获取最新内容。
个人成长离不开各位的关注,你的关注就是我继续前行的动力。