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