python中打印一个字符串中有多少个单词

时间:2019-04-23 18:14:25   收藏:0   阅读:347

1.for实现:

str1 = str.strip(input("please input a str:"))
n = 1
i = 0
for i in range(len(str1)-1):
if str.isalnum(str1[i]) != str.isalnum(str1[i+1]):
n += 1
else:
i += 1
print("字符个数为:",int((n+1)/2))

2.while实现:
# str1 = str.strip(input("please input a str:"))
# n = 0
# i = 0
# while i < len(str1):
# while str1[i] != " ":
# i += 1
# if i == len(str1):
# break
# n += 1
# if i == len(str1):
# break
# while str1[i] == " ":
# i += 1
# print(n)




+++++++++++++++++++++++++++++++++++++++++++++++++++++

# str1 = str.strip(input("please input a str:"))
# n = 1
# i = 0
# while i < len(str1):
# while str1[i] == ‘ ‘:
# n += 1
# i += 1
# break
# while str1[i] == ‘ ‘:
# i += 1
# i += 1
# print(n)


原文:https://www.cnblogs.com/chen-wg/p/10757740.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!