python - 统计word中大写和小写字符的个数

标签 python string list python-3.x

def Charater(): 
    UpperCount = 0 
    LowerCount = 0
    word = input('Enter a word: ')
    for letter in word:
        if letter == letter.upper 
            UpperCount = UpperCount + 1
            return UpperCount
        else:
            LowerCount = LowerCount + 1
            return LowerCount

print(Charater())

如果这看起来很糟糕,请不要评判我。但作为一个初学者,我试图让代码计算用户输入的单词中有多少个大写和小写字符。每次我这样做都会返回 1。(这可能是我的 if 语句)。有人可以指出问题并告诉我如何解决吗?

最佳答案

您可以使用mapstr.isupperstr.islower分别查找大写和小写字符的数量:

>>> my_word = "HelLo WorLd"
>>> lower_count = sum(map(str.islower, my_word))
>>> lower_count
6

>>> upper_count = sum(map(str.isupper, my_word))
>>> upper_count
4

关于python - 统计word中大写和小写字符的个数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42880277/

相关文章:

Python 内置函数 "compile"。它是干什么用的?

python - 为什么我无法在代码中确定对象是 DateTime 类型?

C:如何为字符串分配准确数量的字节?

Python:如何快速堆叠数据框中某一列的所有数组?

python - 如何更改kivy中的弹出颜色

c - 搜索字符串的程序中出现段错误

android - 在此行 : 找到多个注释

python - 如何检查节点列表是否已包含在列表列表中的列表中?

c - ,C, 尝试删除列表中的最后一个元素时崩溃

python - 使用列表理解生成带计数器的二维数组