python如何统计单词个数

可以使用Python的split()方法将字符串分割成单词,并使用len()函数统计单词个数。

例如,下面的代码可以统计字符串s中单词的个数:

s = “Hello world! This is a sentence.”
words = s.split()
num_words = len(words)
print(“单词个数:”, num_words)
输出结果为:
单词个数: 7

阅读剩余
THE END