1. 输入一行字符,统计其中有多少个单词,每两个单词之间以空格隔开。如输入: This is a c++ program. 输出:There are 5 words in the line.
代码:
str = input('输入字符串:')
li = str.split()
print('There are %d words in the line.' %(len(li)))
运行结果:
2. 给出一个字符串,在程序中赋初值为一个句子,例如"he threw three free throws",自编函数完成下面的功能:
求出字符列表中字符的个数(对于例句,输出为26);
代码:
str = input()
print(len(str))
运行结果: