python-8, 编写一个函数,实现统计字符串中单词的个数并返回

代码如下:
 

def func8(str):
    count = 0
    for i in str:
        if 'a' <= i <= 'z' or 'A' <= i <= 'Z':
            count += 1
    return count
s = str(input("请输入一个字符串:"))
count = func8(s)
print("字符串中单词的个数:%d" % count)

效果如下:

猜你喜欢

转载自blog.csdn.net/nikezhenhaokan/article/details/130241206