求字母的个数 求单词的个数

Posted wenm1128

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求字母的个数 求单词的个数相关的知识,希望对你有一定的参考价值。

#习题7:求一个字符串中的字母个数函数需判断传入参数的类型。必须使用ascii来判断是否字母

s = "I am a 12 years old boy!"

def letter_num(s):
    count=0
    if isinstance(s,str):
        for i in s:
            if (ord(i)>=97 and ord(i)<=121) or (ord(i)>=65 and ord(i)<=90):
                count+=1
    else:
        print("请传入一个字符串!")
        return -1
    return count


print(letter_num(s))

 

以上是关于求字母的个数 求单词的个数的主要内容,如果未能解决你的问题,请参考以下文章