温馨提示×

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

小亿
133
2023-12-25 11:19:38
栏目: 编程语言

你可以使用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个小写字母。

0