温馨提示×

python怎么统计字符串中小写字母个数

小亿
492
2023-12-25 11:19:38
栏目: 编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

你可以使用Python的内置方法islower()count()来统计字符串中小写字母的个数。下面是一个示例代码:

def count_lowercase(string):
    count = 0
    for char in string:
        if char.islower():
            count += 1
    return count

# 示例用法
string = "Hello World"
lowercase_count = count_lowercase(string)
print(lowercase_count)

运行这段代码,输出结果为:

8

这表示字符串中包含8个小写字母。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:Python怎么判断字符串是否全为大写或小写字母

0