str = input("请您输入一串字符串:")
str1 = str.strip() # 去掉字符串前后空格
index = 0
count = 0
while index < len(str1):
while str1[index] != " ": # 当不是空格是,下标加1
index += 1
if index == len(str1): # 当下标大小跟字符串长度一样时结束当前循环
break
count += 1 # 遇到空格加1
if index == len(str1): # 当下标大小跟字符串长度一样时结束当前循环
break
while str1[index] == " ": # 当有两个空格时,下标加1,防止以一个空格算一个单词
index += 1
print("输入的字符串中一共有count = %d个单词" % count)
Python输入一串字符串,输出字符串单词的个数
猜你喜欢
转载自blog.csdn.net/yihong_li/article/details/81121128
今日推荐
周排行