Python零基础学习代码实践 —— 提取字符串里面的单词数


str = input()
str1 = str.strip()
index = 0
count = 0
while index < len(str1):
    while str1[index] != " ":
        index += 1
        if index == len(str1):
            break
    count += 1
    if index == len(str1):
        break
    while str1[index] == " ":
        index += 1
print(count)


执行结果

C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/Administrator/PycharmProjects/untitled/day005/提取字符转里面的单词数.py

4

Process finished with exit code 0


上一篇:《逻辑与计算机设计基础(原书第5版)》——1.4 算术运算


下一篇:TiDB 源代码学习: ranger 模块代码分析