Linux 拨号vps windows公众号手机端

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

lewis 8年前 (2017-06-06) 阅读数 8 #程序编程
文章标签 python

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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门