1. 程式人生 > >python中打印一個字符串中有多少個單詞

python中打印一個字符串中有多少個單詞

++ else == 字符 NPU 打印 print input n+1

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)


python中打印一個字符串中有多少個單詞